code_junkie

Zip Folder is always corrupted after downloaded

Nov 13th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. protected void gvFiles_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
  2. {
  3. string fileUrl = String.Empty;
  4.  
  5. if(e.CommandName.Equals("DownloadFile"))
  6. {
  7. fileUrl = e.CommandArgument as String;
  8. string fileName = Path.GetFileName(fileUrl);
  9.  
  10. Response.AppendHeader("content-disposition",
  11. "attachment; filename=" + fileName);
  12. Response.ContentType = "application/zip";
  13.  
  14. Response.WriteFile(fileUrl);
  15. Response.End();
  16.  
  17.  
  18.  
  19. }
  20. }
  21.  
  22. private void BindData()
  23. {
  24. List<SampleFile> files = new List<SampleFile>();
  25.  
  26. for(int i=1;i<=3;i++)
  27. {
  28. SampleFile sampleFile = new SampleFile();
  29. sampleFile.Name = "File " + i;
  30. sampleFile.Url = Server.MapPath("~/Files/File"+i+".txt");
  31. files.Add(sampleFile);
  32. }
  33.  
  34. SampleFile file = new SampleFile();
  35. file.Name = "Zip File";
  36. file.Url = Server.MapPath("~/Files/WebSiteNestedMasters.zip");
  37. files.Add(file);
  38. gvFiles.DataSource = files;
  39. gvFiles.DataBind();
  40. }
  41.  
  42. <a href='<% Eval("Url")) %>'>download</a>
  43.  
  44. fileUrl = e.CommandArgument as String;
  45. string fileName = Path.GetFileName(fileUrl);
  46.  
  47. FileStream fs = new FileStream(fileUrl, FileMode.Open);
  48. byte[] buffer = new byte[fs.Length];
  49. fs.Read(buffer, 0, (int) fs.Length);
  50. fs.Close();
  51.  
  52. Response.Clear();
  53. Response.AppendHeader("content-disposition",
  54. "attachment; filename=" + fileName);
  55. Response.ContentType = "application/octet-stream";
  56. Response.AppendHeader("content-length", buffer.Length.ToString());
  57.  
  58. Response.BinaryWrite(buffer);
  59. Response.Flush();
  60. Response.End();
  61.  
  62. if(e.CommandName.Equals("DownloadFile"))
  63. {
  64. fileUrl = e.CommandArgument as String;
  65. string fileName = Path.GetFileName(fileUrl);
  66.  
  67. FileInfo info = new FileInfo(fileUrl);
  68.  
  69. Response.Clear();
  70. Response.ClearContent();
  71. Response.ClearHeaders();
  72. Response.Buffer = true;
  73.  
  74. Response.AppendHeader("Content-Length",info.Length.ToString());
  75. Response.ContentType = GetContentType(fileUrl);
  76. Response.AppendHeader("Content-Disposition:", "attachment; filename=" + fileName);
  77. Response.TransmitFile(fileUrl);
  78. Response.Flush();
  79. Response.End();
  80. }
Add Comment
Please, Sign In to add comment