【发布时间】:2015-10-22 12:41:14
【问题描述】:
最终目标: 用户正在将大量不同大小的文件上传到我的网站。而且我不想在磁盘上重复文件。
我一直使用的解决方案是文件上传时的简单 SH1 哈希。使用这样的代码:
public static string HashFile(string FileName)
{
using (FileStream stream = File.OpenRead(FileName))
{
SHA1Managed sha = new SHA1Managed();
byte[] checksum = sha.ComputeHash(stream);
string sendCheckSum = BitConverter.ToString(checksum).Replace("-",string.Empty);
return sendCheckSum;
}
}
这对于较小的文件“工作”很好,但是当文件为 30gb 时它会很痛苦。所以我想在从客户端接收文件时对文件进行哈希处理。我从客户端以“块”的形式获取文件,块的大小并不总是静态的。
接收文件的代码。
int chunk = context.Request["chunk"] != null ? int.Parse(context.Request["chunk"]) : 0;
int chunks = context.Request["chunks"] != null ? int.Parse(context.Request["chunks"]) : 0;
string fileName = context.Request["name"] != null ? context.Request["name"] : string.Empty;
HttpPostedFile fileUpload = context.Request.Files[0];
string fullFilePath = Path.Combine(SiteSettings.UploadTempFolder, fileName);
using (var fs = new FileStream(fullFilePath, chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
**// Here i want the hash, when i have the file data in memory.**
}
【问题讨论】:
-
这似乎是最好的方式@Sinatr,将其作为回复发布:)
-
你成功了吗?然后自己为未来的访问者发布答案。
-
是的,我想你想要 presius 互联网积分 :)