How to download file in asp.net

Hi Friends,

Today I am going to describe file download functionality in asp.net.

we can download file using different methods in asp.net.

Today I am going to describe Response.TransmitFile method of asp.net

Lets see how can we do it


public void Downloadfile(string fileName, string filePath )
{
    FileInfo file = new FileInfo(filePath + "\\" + fileName);
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.TransmitFile(filePath + "\\" + fileName);
    Response.Flush();
    Response.End(); 
}

You can download file easily by using above function.
Note:
1. You need to pass fileName and filePath parameters
2. Here, I have used general content type “application/octet-stream”. Change it as per your requirement
3. Add reference for System.IO

Hope this will help you !!!!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>