发送文件覆盖的HttpContext导致浏览器挂起
-
23-08-2019 - |
题
我发送文件到使用的HttpHandler用户。在所有的浏览器,查看后/下载文件至少一次,在随后的意见/下载挂起浏览器/应用程序。下面是代码:
private void TransmitFile(HttpContext context, string filePath, string downloadName, bool forceDownload)
{
if (File.Exists(filePath))
{
string fileName = System.IO.Path.GetFileName(filePath);
string extension = Path.GetExtension(filePath);
FileInfo fileInfo = new FileInfo(filePath);
// set the response info/headers
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
if (forceDownload)
{
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadName.Replace(" ", "_") + extension);
context.Response.BufferOutput = false;
}
string type = "";
// set known types based on file extension
if (extension != null)
{
switch (extension.ToLower())
{
case ".tif":
case ".tiff":
type = "image/tiff";
break;
case ".jpg":
case ".jpeg":
type = "image/jpeg";
break;
case ".gif":
type = "image/gif";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case "pdf":
type = "Application/pdf";
break;
case "png":
type = "image/png";
break;
case "bmp":
type = "image/bmp";
break;
default:
type = "application/octet-stream";
break;
}
}
context.Response.ContentType = type;
context.Response.TransmitFile(filePath);
context.Response.Flush();
}
else
{
Immersive.Diagnostics.Log.Warn("Requested file does not exist: " + filePath, this);
Immersive.Diagnostics.Log.Warn("", this);
}
}
我已阅读,调用Response.Close()到Response.End和()是不是一个好主意?曾经尝试都留下,除去,和它仍然发生。
修改强>
似乎已的TransmitFile已知问题。更详尽的解释,可以发现: HTTP:// WWW。 improve.dk/blog/2008/03/29/response-transmitfile-close-will-kill-your-application
我删除的TransmitFile和改变为WriteFile的,现在完美。
context.Response.WriteFile(filePath);
context.Response.Flush();
context.Response.Close();
解决方案
这可能是一个已知的问题,如果你从下载服务器运行Windows Server 2003 SP1。
下面是一种修复: http://support.microsoft.com/kb/902780
此外,检查是否已OutputCache
页面上启用,如果是这样,请尝试重新下载离不开它了。
其他提示
你不使用缓冲的原因吗?你是同花顺(),即使你不使用它荷兰国际集团它。是的,我想你也应该在同花顺()之后做一个到Response.End()。
让我知道这是否正常工作,也许我可以运行一些我自己的测试。
不隶属于 StackOverflow