2010-12-17 4 views
1

나는 수천 장의 그림이 포함 된 ZIP 파일을 즉석에서 만들려고합니다.HttpContext.Current.Response 다운로드 관리자가 시작한 후 파일 쓰기

공공 정적 무효 CompressAndSendFiles (목록 파일) { HttpContext.Current.Response.ClearContent();

// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header 
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"Jpeg Package " 
              + DateTime.Now.ToString("MM-dd-yyyy hh-mm-ss") + ".zip\""); 

    // Add the file size into the response header 
    //HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); 

    // Set the ContentType 
    HttpContext.Current.Response.ContentType = ReturnHttpContentType("download.zip"); 

    ZipOutputStream oZipStream = 
        new ZipOutputStream(HttpContext.Current.Response.OutputStream); 

    try 
    { 
     foreach (string file in files) 
     { 


      FileInfo fInfo = new FileInfo(file); 
      ZipEntry oZipEntry = new ZipEntry(fInfo.Name); 
      oZipStream.PutNextEntry(oZipEntry); 
      byte[] buffer = File.ReadAllBytes(file); 
      oZipStream.Write(buffer, 0, buffer.Length); 

      //oZipStream.Flush(); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     oZipStream.Finish(); 
     oZipStream.Close(); 
     oZipStream.Dispose(); 

    } 
    HttpContext.Current.Response.OutputStream.Flush(); 
    HttpContext.Current.Response.End(); 
} 

파일 수가 커지지 않으면 모든 것이 좋습니다.

내 질문에 : 다운로드를 시작하고 (클라이언트 쪽 팝업에서 다운로드 관리자에게 맡기십시오) 스트림을 작성하는 방법이 있습니까?

w3wp.exe (IIS) 프로세스를 모니터링했는데 데이터가 스트림 대신 메모리에 기록되는 것으로 보입니다. w3wp.exe 메모리 사용량이 일정한 수치를 초과하면 메모리를 해제하고 아무 것도 발생하지 않습니다 (다운로드하지 않음).

미리 감사드립니다.

답변

2

사용 해보셨습니까?

HttpContext.Current.Response.BufferOutput = false; 
+0

해피 250k 담당자! (글쎄, 아직은 아니지만, 때 안타 :)) – RCIX

+0

고마워요! C# 신의 이중성은 없습니다! –

+0

안녕 Arash, 나도 같은 문제가있어. 이 문제를 어떻게 해결했는지 최종 코드를 보여 주시겠습니까? –

1

아는 한, 보관 중에는 스트리밍 할 수 없습니다. 먼저 압축 한 다음 스트리밍해야합니다. 결과적으로 최종 zip이 매우 클 경우 서버의 메모리가 마비됩니다.

내가 끝내는 것은 임시 zip 파일에 쓰고 그 임시 파일을 스트리밍하는 것입니다.

관련 문제