About      Contributors      FeedBack      Forum
Register      Login

Venkateswarlu.co.in


File downloading in ASP.Net using C#

For downloading a file in ASP.Net we application provides a class WebClient.

This class contains method called DownloadData(), with file path as input parameter.

WebClient class is avaliable with anmespace System.Net.

So to download any file in ASP.Net using C# import this namespace

Namespace

using System.Net;

File

C#  code to download a file

protected void btnDowmLoad_Click(object sender, EventArgs e)
{
    try
    {
        string strURL=txtFileName.Text;
        WebClient req=new WebClient();
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ClearContent();
        response.ClearHeaders();
        response.Buffer= true;
        response.AddHeader("Content-Disposition","attachment;filename=\"" + Server.MapPath(strURL) + "\"");
        byte[] data=req.DownloadData(Server.MapPath(strURL));
        response.BinaryWrite(data);
        response.End();
    }
    catch(Exception ex)
    {
     }
}

Download





APPSC Jobs Notifications

AP DSC 2013 Notification (20,508 posts)

APPSC - Panchayat Raj Recruitment 2013 (2,667 posts)


Email Subscription


Email   
SQL Server Interview Questions SSRS Interview Questions C#.Net Interview Questions WCF Interview Questions SharePoint Interview Questions