2010-03-16 3 views
4
string fileName = "test.zip"; 
string path = "c:\\temp\\"; 
string fullPath = path + fileName; 
FileInfo file = new FileInfo(fullPath); 

Response.Clear(); 
Response.ClearContent(); 
Response.ClearHeaders(); 
Response.Buffer = true; 
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName); 
Response.AppendHeader("content-length", file.Length.ToString()); 
Response.ContentType = "application/x-compressed"; 
Response.TransmitFile(fullPath); 
Response.Flush(); 
Response.End(); 

실제 zip 파일 c : \ temp \ test.zip은 원하는대로 유효하고 유효합니다. c : \ temp \ 디렉토리로 이동하여 test.zip 파일을 두 번 클릭하십시오. 바로 열립니다.ASP.NET 다운로드 압축 용 zip 파일 생성 : 압축 된 압축 폴더가 유효하지 않거나 손상됨

내 문제는 다운로드와 관련되어있는 것 같습니다. 위의 코드는 문제없이 실행됩니다. 파일 다운로드 대화 상자가 표시됩니다. 나는 저장하거나 열어 둘 수 있습니다. 대화 상자에서 파일을 열려고하면 파일을 저장 한 다음 열어 봅니다. 다음과 같은 대화 메시지가 나타납니다.

압축 된 (압축 된) 폴더가 잘못되었거나 손상되었습니다. 내가 해봤 Response.ContentType를 들어

:

응용 프로그램/X-압축 응용 프로그램/X-ZIP 압축 응용 프로그램/X-gzip으로 compresse 응용 프로그램/octet-stream을은 응용 프로그램/

을 압축

http://www.codeplex.com/DotNetZip

Ionic.zip

을 : 사용

zip 파일은 (인해 직접 만든 파일을 열 수있는 내 능력을 내가 잘 작동 확신 있음) 일부 이전의 코드로 생성되는

+0

다운로드 한 파일의 크기가 원본과 같은가요? – CResults

답변

21

이것은 효과가 있습니다. 나는 왜 그런지 모르지만 그랬다.

string fileName = "test.zip"; 
string path = "c:\\temp\\"; 
string fullPath = path + fileName; 
FileInfo file = new FileInfo(fullPath); 

Response.Clear(); 
//Response.ClearContent(); 
//Response.ClearHeaders(); 
//Response.Buffer = true; 
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
//Response.AppendHeader("Content-Cength", file.Length.ToString()); 
Response.ContentType = "application/x-zip-compressed"; 
Response.WriteFile(fullPath); 
//Response.Flush(); 
Response.End(); 
+0

또한 여기 일했습니다, 고마워요. 문제가 Response.AppendHeader ("Content-Length", file.Length.ToString()) 및 Response.Flush() 둘 중 하나와 함께 작동하지 않았지만 둘 모두없이 작동하는 것 같습니다. 누군가 설명 할 수 있니? 어쨌든 Jason 고맙습니다 :) – Nei