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);
}
}
응답을 콜로스 () 및 응답 ()로 부르는 전화 ()는 좋은 생각이 아니십니까? 떠나고 제거하려고 시도했지만 여전히 발생합니다.
편집하다:
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();
해결책
Runs Windows Server 2003 SP1에서 다운로드하는 서버가 알려진 문제 일 수 있습니다.
다음은 핫픽스입니다. http://support.microsoft.com/kb/902780
또한 가지고 있는지 확인하십시오 OutputCache
페이지에서 활성화하면 그렇다면 다운로드없이 다시 다운로드하십시오.
다른 팁
버퍼링을 사용하지 않는 이유가 있습니까? 당신은 그것을 사용하지 않더라도 그것을 홍조 ()하고 있습니다. 그리고 네, 플러시 () 후에도 응답을해야한다고 생각합니다.
이것이 작동하는지 알려 주시고 어쩌면 내 자신의 테스트를 실행할 수 있습니다.
제휴하지 않습니다 StackOverflow