Friday, August 31, 2012

File downloading in ASP.Net using C# code

using System.Net;
protected void btnDowmLoadFile_Click(object sender, EventArgs e)
{
    try
    {
        string strUrl = “path/filename.xls”;
        WebClient request=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[] dataFile= request.DownloadData(Server.MapPath(strUrl));
        response.BinaryWrite(dataFile);
        response.End();
    }
    catch(Exception ex)
    {
     }
}

No comments: