Saturday, February 18, 2012

Create file download option using ASP.NET and C#


Create file download using ASP.Net
This post is for creation of download link using ASP.NET and C# Technology. A download link is very much necessary to create a typical web application.




Step 1) Copy and paste the below code in your ASP.Net page. Change the Directory location according to your server setting.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class File_download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        DirectoryInfo directory = new DirectoryInfo("D:\\files");
        int counter = 0;
        foreach (FileInfo file in directory.GetFiles())
        {
            HyperLink link = new HyperLink();
            link.ID = "Link" + counter++;
            link.Text = file.Name;
            link.NavigateUrl = "download.aspx?name=" + file.Name;

            Page.Controls.Add(link);
            Page.Controls.Add(new LiteralControl("<br/>"));
        }
    }
}


Step 2)Create another ASP.NET file and give filename download.aspx and put below code into it.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string fileName = Request.QueryString["name"].ToString();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.TransmitFile("D:\\files\\" + fileName);
        Response.End();

    }
}




10 comments:

  1. Where do I place the code in Step 1??

    My program gives me build error.

    VS version: 2010 Professional.
    OS Version: Win XP SP3

    I am using C sharp.net.

    Thanks

    ReplyDelete
  2. but when i m adding my file location its showing error
    C:\Users\hp\Desktop\testWebSite\filefolder\

    ReplyDelete
  3. could u please explain step by step how to apply this in website pls.....do this favour..!!

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Blogger Anup Shukla said...
    hye i did all above process bt i am getting an error "The device is not ready at this line of code - Response.TransmitFile("D:\\files\\" + fileName);"

    please help me out.

    Thanks in advance.

    ReplyDelete
  7. http://martinkabraham.blogspot.ae/2013/11/android-browser-file-down-load.html

    ReplyDelete
  8. The size parameter must be between zero and the maximum Int32 value.
    Parameter name: size
    Actual value was 3166778307

    I got an error when i tried to download file of size 3 gb .

    Please Help me out of this !

    Thanks

    ReplyDelete
  9. Your Net Worth Statement is an assessment of your current gross value minus your current Quavo Net Worth. In the previous article of this series we looked at how to determine your current gross value, in this one we look at how to calculate your current liabilities. When determining your gross value it was suggested that when there was some leeway in absolute value, you should be conservative and estimate on the lower side of the range of values.

    ReplyDelete