How to download a file from Server folder
Now we are going to see how to download a file or image from the Server folder ie: showing file/open dialog box. For this we have to use Response object to show or get the file.
Sample Code:
private void WriteFileToResponse() { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.MapPath("~/Download/" + FileName)); // Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; //Write the file to the Response Output stream without buffering. //This is new to ASP.NET 2.0. Prior to that use WriteFile. Response.TransmitFile(Server.MapPath("~/Download/" + FileName)); Response.Flush(); Response.End(); }
Read more »