2012-03-05 2 views
0

.zip 파일을 다운로드하는 데 webmethod를 사용하고 있지만 파일을 다운로드 할 수 없습니다. 오류없이 잘 실행 내 코드는 코드는 다음과 같습니다 : 내가 VS2008 모든 솔루션 감사를 사용하고
파일이 웹 메서드에서 다운로드되지 않았습니다.

[WebMethod] 
    public static void DownloadExtension(string ExtensionPath) 
    { 
     string filepath = HttpContext.Current.Server.MapPath(ExtensionPath); 
     FileInfo file = new FileInfo(filepath); 
     if (file.Exists) 
     { 
      HttpContext.Current.Response.ClearContent(); 
      HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
      HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); 
      HttpContext.Current.Response.ContentType = ReturnExtension(file.Extension.ToLower()); 
      HttpContext.Current.Response.TransmitFile(file.FullName); 
      HttpContext.Current.Response.End(); 
     } 
    } 

    private static string ReturnExtension(string fileExtension) 
    { 
     switch (fileExtension) 
     {    
      case ".zip": 
       return "application/zip"; 
      default: 
       return "application/octet-stream"; 
     } 
    } 


. u는이 method..it을 시도 할 수 있습니다

답변

1

는 .ASHX 핸들러 파일을 통해 파일을 다운로드 ..

  protected void Page_Load(object sender, EventArgs e) 
{ 
    StartZip(Server.MapPath("directory name"), "filename");  
} 

protected void StartZip(string strPath, string strFileName) 
{ 
    MemoryStream ms = null; 
    Response.ContentType = "application/octet-stream"; 
    strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' '); 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip"); 
    ms = new MemoryStream(); 
    zos = new ZipOutputStream(ms); 
    strBaseDir = strPath + "\\"; 
    addZipEntry(strBaseDir); 
    zos.Finish(); 
    zos.Close(); 
    Response.Clear(); 
    Response.BinaryWrite(ms.ToArray()); 
    Response.End(); 
} 

protected void addZipEntry(string PathStr) 
{ 
    DirectoryInfo di = new DirectoryInfo(PathStr); 
    foreach (DirectoryInfo item in di.GetDirectories()) 
    { 
     addZipEntry(item.FullName); 
    } 
    foreach (FileInfo item in di.GetFiles()) 
    { 
     FileStream fs = File.OpenRead(item.FullName); 
     byte[] buffer = new byte[fs.Length]; 
     fs.Read(buffer, 0, buffer.Length); 
     string strEntryName = item.FullName.Replace(strBaseDir, ""); 
     ZipEntry entry = new ZipEntry(strEntryName); 
     zos.PutNextEntry(entry); 
     zos.Write(buffer, 0, buffer.Length); 
     fs.Close(); 
    } 
} 
1

시도를 ... 나를 위해 잘 작동 그것이 당신이 도움이되기를 바랍니다.

관련 문제