gpt4 book ai didi

c# - 创建自定义文件下载。 MD5 哈希值不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:27 25 4
gpt4 key购买 nike

我使用 Asp.Net MVC 创建了一个文件下载器。内置 Asp.Net MVC 函数的问题在于它们无法处理超大文件下载,并且在某些浏览器中它们不会弹出另存为对话框。所以我使用来自 msdn http://support.microsoft.com/kb/812406 的文章自己滚动.现在的问题是文件下载完美,但 MD5 校验和不匹配,因为服务器上的文件大小与下载的文件大小略有不同(尽管 1000 次测试表明下载执行得很好)。这是代码:

public class CustomFileResult : ActionResult
{
public string File { get; set; }

public CustomFileResult(string file)
{
this.File = file;
}

public override void ExecuteResult(ControllerContext context)
{
Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file name.
string filename = System.IO.Path.GetFileName(this.File);

try
{
// Open the file.
iStream = new System.IO.FileStream(this.File, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);


// Total bytes to read:
dataToRead = iStream.Length;

context.HttpContext.Response.ContentType = "application/octet-stream";
context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (context.HttpContext.Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);

// Write the data to the current output stream.
context.HttpContext.Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
context.HttpContext.Response.Flush();

buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
context.HttpContext.Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
context.HttpContext.Response.Close();
}
}
}

和执行:

return new CustomFileResult(file.FullName);

最佳答案

尝试使用

Response.TransmitFile(string fileName) 

方法。

它真的很好,并且有一些东西可以避免 OutOfMemory 预期。

http://msdn.microsoft.com/en-us/library/12s31dhy(v=vs.80).aspx

关于c# - 创建自定义文件下载。 MD5 哈希值不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4665006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com