asp.net에서 파일 스트리밍은 Firefox에서 작동하지만 인터넷 익스플로러에서 작동합니다.

StackOverflow https://stackoverflow.com/questions/1209002

문제

ASP.NET 페이지에서 ZIP 파일을 동적으로 생성 한 다음 스트림을 응답으로 보냅니다.

Firefox에서는 이름이 지정된 파일을 다운로드 할 수 있습니다 Images.zip. 올바르게 작동합니다. Internet Explorer 7에서는 ZipExport.aspx 또는 일반 핸들러에있는 경우 ZipExport.ashx 그리고 서버에서 찾을 수없고 실패한다고 말합니다.

내 코드는 다음과 같습니다.

Response.BufferOutput = true;
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();
Response.Cache.SetMaxAge(System.TimeSpan.Zero);
ZipFile zip = new ZipFile();
zip.AddFile(Server.MapPath("sample1.png"));
zip.Save(Response.OutputStream);

특정 파일을 위해 httphandler를 만들고 IIS에 등록하고 싶지 않습니다.

내 컨텐츠 방지 헤더를 무시한 것에 대해 단순한 내용이 없거나 인터넷 익스플로러가 잘못 되었습니까?

편집 : 나는이 라인을 제거했고 일이 효과가있었습니다.

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

편집 : 누구나 관심이있는 경우 작업 코드가 있습니다.

public void ProcessRequest(HttpContext context)
{  
    context.Response.Clear();
    context.Response.BufferOutput = false;
    context.Response.ContentType = "application/octet-stream";
    context.Response.AddHeader("content-disposition", 
        "attachment; filename=ChartImages.zip");
    context.Response.Cache.SetNoServerCaching();
    context.Response.Cache.SetMaxAge(System.TimeSpan.Zero);
    using(ZipFile zip = new ZipFile())
    {
        zip.AddFile(context.Server.MapPath("sample1.png"));
        zip.Save(context.Response.OutputStream);
    }
    context.ApplicationInstance.CompleteRequest();
}
도움이 되었습니까?

해결책

바꾸다 Response.End ~와 함께 HttpContext.Current.ApplicationInstance.CompleteRequest

이 컷 다운 버전을 시도하십시오.

Response.Clear();
Response.BufferOutput = false;

Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
using(ZipFile zip = new ZipFile())
{
  zip.AddFile(Server.MapPath("sample1.png"));
  zip.Save(Response.OutputStream);
}
HttpContext.Current.ApplicationInstance.CompleteRequest();

실패하면 Microsoft Fiddler를 사용하여 다른 것이 무엇인지 확인합니다.

다른 팁

당신은 만들어야합니다 Ashx 처리기 그에 대한. 대신 콘텐츠 유형의 '응용 프로그램/zip'을 사용해 보셨습니까?

응답 대신 clearheaders () 대신 전체를 수행하십시오 응답., 그 후에는 a 응답 ()

방금 같은 문제 (그리고 수정) 감사합니다.

미래의 검색자를 도울 수있는 한 가지 요점은 HTTPS 사이트에서만 문제가 발생했다는 것입니다. 내 HTTP 로컬 서버에서 코드가 정상적으로 실행되었습니다.

나는 https를 사용하면 어쨌든 캐시되지 않으므로 "if (request.issecureconnection)"조건으로 동봉 할 수 있습니다.

나는 zipfile 클래스를 사용한 적이 없다. 파일을 보낼 때 나는 response.binaryWrite ()를 사용한다.

//Adds document content type
context.Response.ContentType = currentDocument.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AddHeader("content-disposition", "attachment;filename=\"" + currentDocument.Name + "\"");



//currentDocument.Document is the byte[] of the file
context.Response.BinaryWrite(currentDocument.Document);

context.Response.End();

방금 같은 문제를 겪고 수정했습니다.

응답 .clear (); 응답 .bufferoutput = false;

                    Response.ContentType = "application/zip";
                    //Response.AddHeader("content-disposition", "inline; filename=\"" + ArchiveName + "\"");
                    Response.AddHeader("content-disposition", "attachment; filename=\"" + ArchiveName + "\"");
                    zipFile.Save(Response.OutputStream);
                   // Response.Close();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    Response.Clear();
                    Response.End();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top