Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void gvFiles_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
- {
- string fileUrl = String.Empty;
- if(e.CommandName.Equals("DownloadFile"))
- {
- fileUrl = e.CommandArgument as String;
- string fileName = Path.GetFileName(fileUrl);
- Response.AppendHeader("content-disposition",
- "attachment; filename=" + fileName);
- Response.ContentType = "application/zip";
- Response.WriteFile(fileUrl);
- Response.End();
- }
- }
- private void BindData()
- {
- List<SampleFile> files = new List<SampleFile>();
- for(int i=1;i<=3;i++)
- {
- SampleFile sampleFile = new SampleFile();
- sampleFile.Name = "File " + i;
- sampleFile.Url = Server.MapPath("~/Files/File"+i+".txt");
- files.Add(sampleFile);
- }
- SampleFile file = new SampleFile();
- file.Name = "Zip File";
- file.Url = Server.MapPath("~/Files/WebSiteNestedMasters.zip");
- files.Add(file);
- gvFiles.DataSource = files;
- gvFiles.DataBind();
- }
- <a href='<% Eval("Url")) %>'>download</a>
- fileUrl = e.CommandArgument as String;
- string fileName = Path.GetFileName(fileUrl);
- FileStream fs = new FileStream(fileUrl, FileMode.Open);
- byte[] buffer = new byte[fs.Length];
- fs.Read(buffer, 0, (int) fs.Length);
- fs.Close();
- Response.Clear();
- Response.AppendHeader("content-disposition",
- "attachment; filename=" + fileName);
- Response.ContentType = "application/octet-stream";
- Response.AppendHeader("content-length", buffer.Length.ToString());
- Response.BinaryWrite(buffer);
- Response.Flush();
- Response.End();
- if(e.CommandName.Equals("DownloadFile"))
- {
- fileUrl = e.CommandArgument as String;
- string fileName = Path.GetFileName(fileUrl);
- FileInfo info = new FileInfo(fileUrl);
- Response.Clear();
- Response.ClearContent();
- Response.ClearHeaders();
- Response.Buffer = true;
- Response.AppendHeader("Content-Length",info.Length.ToString());
- Response.ContentType = GetContentType(fileUrl);
- Response.AppendHeader("Content-Disposition:", "attachment; filename=" + fileName);
- Response.TransmitFile(fileUrl);
- Response.Flush();
- Response.End();
- }
Add Comment
Please, Sign In to add comment