0

我有文件存储在数据库中。我没有存储在数据库中的名称,所以我正在检查字节数组的前 4 个字节,以确定文件是 pdf 还是 jpg .. 目前我一次只能下载一个文件。我想以 zip 格式下载 DataTable 中的所有文件..(因为我没有文件的原始名称,所以我想给它一个随机名称,并在比较后得到扩展名)

DataRow objRow;
objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(objAdapter);
objAdapter.Fill(objTable);
objRow = objTable.Rows[1];
byte[] objData;
objData = (byte[])objRow["img"];
// Stream stream = new MemoryStream(objData);
var firstNBytes = objData.Take(4);
Byte[] threeBytes = new Byte[] { objData[0], objData[1], objData[2], objData[3] };
var bytesToCompare = Encoding.Unicode.GetBytes("0x25");
bool equal = firstNBytes.SequenceEqual(bytesToCompare);
string hex = BitConverter.ToString(threeBytes);
hex.Replace("-", "");

if (objSfd.ShowDialog() != DialogResult.Cancel)
{
string strFileToSave = objSfd.FileName;

//string strFileToSave = "abc";

if (hex == "25-50-44-46")
{
    strFileToSave += ".pdf";
}
else
{
    strFileToSave += ".jpg";
}

FileStream objFileStream = new FileStream(strFileToSave, FileMode.Create, FileAccess.Write);
objFileStream.Write(objData, 0, objData.Length);
objFileStream.Close();

请帮帮我。

4

1 回答 1

0

目前我一次只能下载一个文件。我想以 zip 格式下载 DataTable 中的所有文件。

尝试像这样迭代您的objTable.Rows

objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(objAdapter);
objAdapter.Fill(objTable);
foreach(DataRow objRow in objTable.Rows)
{
byte[] objData = (byte[])objRow["img"];
var firstNBytes = objData.Take(4);
Byte[] threeBytes = new Byte[] { objData[0], objData[1], objData[2], objData[3] };
var bytesToCompare = Encoding.Unicode.GetBytes("0x25");
bool equal = firstNBytes.SequenceEqual(bytesToCompare);
string hex = BitConverter.ToString(threeBytes);
hex.Replace("-", "");


string strFileToSave = Guid.NewGuid();

if (hex == "25-50-44-46")
{
    strFileToSave += ".pdf";
}
else
{
    strFileToSave += ".jpg";
}

FileStream objFileStream = new FileStream(strFileToSave, FileMode.Create, FileAccess.Write);
objFileStream.Write(objData, 0, objData.Length);
objFileStream.Close();
}
于 2013-10-13T07:08:01.023 回答