[英]c# mvc - Downloading huge files hangs web application
我有一个功能,允许用户通过单击网页上的文件名从服务器下载文件。 我正在使用 ASP.Net MVC5。 任何大小的下载(尝试高达 2GB)适用于我当前的实现。 问题是下载大于 200MB 的文件时,Web 应用程序会挂起,直到完成下载。
当前实现:当用户单击网格中的 fileName 时,它会触发“DocumentController”中的“Download”ActionMethod,从而从服务器下载文件。
然后我想到了两种方法来解决上述问题:
方法1 :使用异步功能。 我是异步编程的新手。 如果这是最好的方法,请您更新我下面的代码以使其行为异步。
public ActionResult Download(string filePath, string fileName)
{
try
{
if(!string.IsNullOrEmpty(filePath))
{
filePath = Path.Combine(baseUrl, filePath);
string contentType = MimeMapping.GetMimeMapping(fileName);
TransmitFile(filePath, fileName);
}
}
catch(Exception ex) { }
return Content("");
}
private void TransmitFile(string fullPath, string outFileName)
{
System.IO.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 to download including its path.
string filepath = fullPath;
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.Clear();
Response.ContentType = MimeMapping.GetMimeMapping(filename);
Response.AddHeader("content-disposition", "attachment; filename=\"" + outFileName + "\"");
Response.AddHeader("Content-Length", iStream.Length.ToString());
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
Response.Close();
}
}
方法 2 :Ajax 调用“下载”ActionMethod。 这对我来说效果不佳。
function downloadFile() {
var data = //Gets data object using some action and works fine
$.ajax({
url: '/Document/IsFileExistOnServer',
data: { 'filePath': data.path },
type: 'GET',
success: function (isFileExist) {
if (typeof isFileExist != "undefined" && isFileExist != null) {
if (isFileExist == "true") {
window.location = '@Url.Action("Download", "Document", new { filePath =' + data.path + ', fileName =' + data.name + ' })';
}
else {
alert("File Not found. Cannot download");
}
}
},
error: function (data) {
alert("Error occured. Please try again.")
}
});
}
ajax 调用的问题是,请求下载文件的视图与另一个控制器 (HomeController) 相关联,单击下载时,当前页面被重定向到这样的链接: http://localhost:59740/Home/详细信息/@Url.Action(下载,文档,新建{filePath=2017/05/TestFile.db,fileName=test1Gb.db})
请有人帮助我最好的方法来做到这一点。 提前致谢 !!!
我认为你应该使用这个:
Response.OutputStream.WriteAsync(buffer, 0, length);
而不是这个:
Response.OutputStream.Write(buffer, 0, length);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.