1
我目前正在编写一些代码来处理和加密上传的文件在ASP中。我的第一次尝试工作(即使是大文件),但需要一段时间在服务器端进行处理。我相信这是因为我一字节地做这个。这里是工作代码...块读取流失败 - 目标数组不够长
using (RijndaelManaged rm = new RijndaelManaged())
{
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
using (ICryptoTransform encryptor = rm.CreateEncryptor(drfObject.DocumentKey, drfObject.DocumentIV))
{
using (CryptoStream cs = new CryptoStream(fs, encryptor, CryptoStreamMode.Write))
{
int data;
while ((data = inputStream.ReadByte()) != -1)
cs.WriteByte((byte)data);
}
}
}
}
如前所述,上面的代码工作正常,但在服务器端处理时很慢。所以我想我会尝试读取块中的字节来加快速度(不知道这是否会造成影响)。我想这个代码...
int bytesToRead = (int)inputStream.Length;
int numBytesRead = 0;
int byteBuffer = 8192;
using (RijndaelManaged rm = new RijndaelManaged())
{
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
using (ICryptoTransform encryptor = rm.CreateEncryptor(drfObject.DocumentKey, drfObject.DocumentIV))
{
using (CryptoStream cs = new CryptoStream(fs, encryptor, CryptoStreamMode.Write))
{
do
{
byte[] data = new byte[byteBuffer];
// This line throws 'Destination array was not long enough. Check destIndex and length, and the array's lower bounds.'
int n = inputStream.Read(data, numBytesRead, byteBuffer);
cs.Write(data, numBytesRead, n);
numBytesRead += n;
bytesToRead -= n;
} while (bytesToRead > 0);
}
}
}
}
然而,在代码中显示 - 当我现在上传大文件时,我得到“目标数组不够长检查destIndex和长度,和阵列的低。边界“错误。我读了关于填充的各种帖子,但即使增加数据字节数组来增加大小仍然会导致错误。
我毫不怀疑我错过了一些明显的东西。谁能帮忙?
感谢,
斑点。此外,'cs.write'行也应该有一个零偏移量 - 但它在所有这些变化之后都可以工作。非常感谢, – Simon