Thursday, September 11, 2014

ASP.Net C# Download file from physical location

Description:
In this example we learn how to download a file physically stored in our local system in asp.net c#
C# Code:
protected void btnDownload_Click(object sender, EventArgs e)
    {
        string strURL = System.IO.Path.GetFullPath("D:\\Files\\Report.xlsx");
        if (File.Exists(strURL))
        {
            WebClient req = new WebClient();
            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.Buffer = true;
            response.AddHeader("Content-Disposition", "attachment;filename=myfile.xlsx");
            byte[] data = req.DownloadData(strURL);
            response.BinaryWrite(data);
            response.End();
        }
        else
        {
            lblStatus.Text = "File Not Found";
        }
    }

No comments:

Post a Comment