2017-02-03 1 views
-4

DotNetZip 및 ASP.NET과 관련된 이상한 상황을 디버깅 중입니다. 짧게 말하자면, 코드에 의해 생성되는 결과 zip 파일은 Firefox에 의해 안정적으로 다운로드되지만 다른 대부분의 브라우저는 간헐적으로 네트워크 오류를 반환합니다. 코드를 살펴본 결과 일반적으로 DotNetZip과 관련된 내용으로 읽 힙니다.때때로 ASP.NET을 사용하여 DotNetZip으로 만든 Zip 파일이 네트워크 오류를 일으킬 수 있습니다.

실마리가 있습니까?

감사합니다.

편집 : 완전한 방법이 있습니다. 내가 언급 한 바와 같이이수록, 그것에 대해 같은 일반의 :

protected void btnDownloadFolders_Click(object sender, EventArgs e) 
{ 
    //Current File path 
    var diRoot = new DirectoryInfo(_currentDirectoryPath); 
    var allFiles = Directory.GetFiles(diRoot.FullName, "*.*", SearchOption.AllDirectories); 
    Response.Clear(); 
    Response.BufferOutput = false; 

    var archiveName = String.Format("{0}-{1}.zip", diRoot.Name, DateTime.Now.ToString("yyyy-MM-dd HHmmss")); 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("content-disposition", "inline; filename=\"" + archiveName + "\""); 

    using (var zip = new ZipFile()) 
    { 
     foreach (var strFile in allFiles) 
     { 
      var strFileName = Path.GetFileName(strFile); 
      zip.AddFile(strFile, 
         strFile.Replace("\\" + strFileName, string.Empty).Replace(diRoot.FullName, string.Empty)); 
     } 

     zip.Save(Response.OutputStream); 
    } 
    Response.Close(); 
} 
+0

당신은이보다 더 많은 정보를 제공 할 필요가 – Sunshine

+0

최소한의, 완전한, 검증 가능한 예를 들어, http://stackoverflow.com/help/mcve을 제공하십시오. 우리는 귀하의 화면을 보거나 네트워크 트래픽을 검사 할 수 없습니다. 나는 당신이 코드 문제에 관해 여기 있다고 가정한다. 코드를 볼 수 없다면 어떻게해야합니까? – Amy

+0

이 문제를 해결하려면'Response.Close();'를'Response.Flush();로 변경하십시오. https://stackoverflow.com/a/736462/481207을 참조하십시오. (Chrome 버전 61. – Matt

답변

1

당신이 content-length를 전송하지 않기 때문에 그것은있을 수 있습니다. 파일을 지정하지 않은 브라우저로 파일을 전송할 때 오류가 발생하는 것을 보았습니다. 따라서 MemoryStream에 zip 파일을 만드십시오. 스트림을 Byte Array에 저장하여 길이를 응답으로 보낼 수도 있습니다. 비록 그것이 당신의 특정 문제를 해결할 것이라고 확신 할 수는 없지만.

byte[] bin; 

using (MemoryStream ms = new MemoryStream()) 
{ 
    using (var zip = new ZipFile()) 
    { 
     foreach (var strFile in allFiles) 
     { 
      var strFileName = Path.GetFileName(strFile); 
      zip.AddFile(strFile, strFile.Replace("\\" + strFileName, string.Empty).Replace(diRoot.FullName, string.Empty)); 
     } 

     //save the zip into the memorystream 
     zip.Save(ms); 
    } 

    //save the stream into the byte array 
    bin = ms.ToArray(); 
} 

//clear the buffer stream 
Response.ClearHeaders(); 
Response.Clear(); 
Response.Buffer = true; 

//set the correct contenttype 
Response.ContentType = "application/zip"; 

//set the filename for the zip file package 
Response.AddHeader("content-disposition", "attachment; filename=\"" + archiveName + "\""); 

//set the correct length of the data being send 
Response.AddHeader("content-length", bin.Length.ToString()); 

//send the byte array to the browser 
Response.OutputStream.Write(bin, 0, bin.Length); 

//cleanup 
Response.Flush(); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
+0

) 지연에 대해 죄송합니다. 알파 영역에서이 솔루션을 사용해보고 효과가 있는지 확인하겠습니다. 감사! –

+0

결과를 저장하기에 더 좋은 곳이 없기 때문에이 코드가 작동합니다. –

+0

참고로,'Response.Close();'또는'Response.End();'를 사용하면 다운로드가 중단됩니다. 대신에'Response.Flush();'를 사용하면'content-length' 헤더가 필요 없습니다. https://stackoverflow.com/a/736462/481207을 참조하십시오. – Matt

관련 문제