ARTICLE

File Download with save as dialog box from browser

Posted by Sanjeeva Reddy Articles | ASP.NET Programming September 14, 2010
Every programmer will get into this scenario where they needs to download a file from browser with a direct button click or downloading a file after some business functional process. I will show you how.
Reader Level:

Every programmer will get into this scenario where they needs to download a file from browser with a direct button click or downloading a file after some business functional process.

Scenario1:  Downloading a file with a button click.

To achieve this
  1. File has to be saved to hard drive on server 

  2. Create a link button and write the following code at the <button>_Click event.(Replace your filename with location at "<filename>")

    System.IO.FileStream fs = null;
    fs = System.IO.File.Open(<filename>,FileMode.Open);
    byte[] btFile = new byte[fs.Length];
    fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    Response.Buffer = true;
    Response.Expires = 0;
    Response.ContentType = "plain/text";
    Response.AddHeader("Content-Type", "plain/text");
    //Response.AddHeader("Content-Length", btFile.Length.ToString);
    Response.AddHeader("Content-Disposition", "attachment;filename=" +    Session["downloadFname"].ToString());
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(btFile);
    Response.End();
Scenario 2: Downloading a file after some business functional process.

Most of the time this could be, you click a button and that button click will execute a function or some process with generates some results . These results have to be downloaded as file.

In this case I'm providing code for generating a tab delimited text file from the data table. And then downloading that file using save as dialog box from the browser with no user interaction.

Generate tab delimited text file from a data table: ( Replace with you filename including path)

StreamWriter sw = new StreamWriter(<filename including path>, false);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
    sw.Write(dt.Columns[i]);
    if (i < iColCount - 1)
    {
        sw.Write("\t");
    }
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
    for (int i = 0; i < iColCount; i++)
    {
        sw.Write(dr[i].ToString().Trim());
        if (i < iColCount - 1)
        {
            sw.Write("\t");
        }
    }
    sw.Write(sw.NewLine);
}
sw.Close();

Downloading file: Here I'm providing multiple of ways of doing the same thing. Please use which ever you are comfortable with.

  1. byte[] fileC = File.ReadAllBytes(<filename>);
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ContentType = "text/plain";
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+ <filename> );
    HttpContext.Current.Response.AppendHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    HttpContext.Current.Response.AppendHeader("Pragma", "public");
    HttpContext.Current.Response.AppendHeader("Expires", "0");
    HttpContext.Current.Response.OutputStream.Write(fileC, 0, fileC.Length);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.End();
    Response.Clear();
    Response.ClearContent();
    Response.ContentType = "text/plain";
    Response.AppendHeader("Content-Disposition", "attachment; filename="+<filename>);
    Response.TransmitFile(recapPath + fname);
    Response.Clear();
    Response.Flush();
    Response.End();

  2. Response.ContentType = "text/txt";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + <filename> + ".txt");
    Page.Response.WriteFile(<filename with extention>);
    Response.End();

  3. System.IO.FileStream fs = null;
    fs = System.IO.File.Open(<filename>, FileMode.Open);
    byte[] btFile = new byte[fs.Length];
    fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    Response.Clear();
    Response.AppendHeader("content-disposition", "attachment; filename=" + <filename>);
    Response.ContentType = "plain/text";
    UTF8Encoding encoding = new UTF8Encoding();
    Response.BinaryWrite(btFile);
    Response.Flush();
    Response.End();

  4. System.IO.FileStream fs = null;
    fs = System.IO.File.Open(<filename>, FileMode.Open);
    byte[] btFile = new byte[fs.Length];
    fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    Response.Buffer = true;
    Response.Expires = 0;
    Response.ContentType = "plain/text";
    Response.AddHeader("Content-Type", "plain/text");
    Response.AddHeader("Content-Length", btFile.Length.ToString);
    Response.AddHeader("Content-Disposition", "attachment;filename=" + <filename>);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(btFile );
    Response.End();

Please remember that the URL for the page where button exists for this functionality shouldn't be secure URL incase if you are using IE6. So, If your clients are going to use the website on IE6 and they want this functionality, make sure that URL doesn't start with  https:// . It should start with http:// because IE6 doesn't flush the file contents properly. 

Hope this article will help you to spend as little time as possible for this functionality. 

Article Extensions
Contents added by Tushar Bharambe on Feb 02, 2013
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<
html xmlns="http://www.w3.org/1999/xhtml"> 
<
head runat="server"> 
    <title></title>
 
</
head> 
<
body> 
    <form id="form1" runat="server">
 
    <div>
 
        <asp:linkbutton id="LinkButtonDownloadDoc" runat="server" text="Download WORD" style="color:Maroon;
 
            font-weightbold;" onclick="LinkButtonDownload_Click" />
 
    </div>
 
    <div>
 
        &nbsp;</div>
 
    <div>
 
        <asp:linkbutton id="LinkButtonDownloadPdf" runat="server" text="Download PDF" style="colorNavy;
 
            font-weightbold;" onclick="LinkButtonDownloadPdf_Click" />
 
    </div>
 
    </form>
 
</
body> 
</
html> 
  

using
 System; 
using
 System.Collections.Generic; 
using
 System.Linq; 
using
 System.Web; 
using
 System.Web.UI; 
using
 System.Web.UI.WebControls; 
 
 
public
 partial class _Default : System.Web.UI.Page 
{
 
    protected void Page_Load(object sender, EventArgs e)
 
    {
 
 
 
    }
 
    protected void LinkButtonDownload_Click(object sender, EventArgs e)
 
    {
 
        Response.ContentType = "Application/msword";
 
        Response.AppendHeader("Content-Disposition""attachment; filename=Test.docx");
 
        Response.TransmitFile(Server.MapPath("~/Files/Test.docx"));
 
        Response.End();
 
    }
 
    protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e)
 
    {
 
        Response.ContentType = "Application/pdf";
 
        Response.AppendHeader("Content-Disposition""attachment; filename=Test_PDF.pdf");
 
        Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf"));
 
        Response.End();
 
    }
 
}
Contents added by abdulkreem salem on Dec 24, 2011
gooooooooooood thank you
COMMENT USING