Downloading Files From Server To Client using ASP.Net C#

Following is the code which will help you to download the file from the server to the local machine



//To Get the physical Path of the file(me2.doc)
string filepath = Server.MapPath("me2.doc");

// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo myfile = new FileInfo(filepath);

// Checking if file exists
if (myfile.Exists)
{
// Clear the content of the response
Response.ClearContent();

// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());

// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);

// End the response
Response.End();
}

private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}


Please Note: If you want to bypass the Open/Save/Cancel dialog you just need to replace LINE1 by the below code

Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name);



Response.TransmitFile VS Response.WriteFile:
1. TransmitFile: This method sends the file to the client without loading it to the Application memory on the server. It is the ideal way to use it if the file size being download is large.
2. WriteFile: This method loads the file being download to the server's memory before sending it to the client. If the file size is large, you might the ASPNET worker process might get restarted.

Hope this helps,Your comments and queries are welcome
--
Thanks and Regards
Meetu Choudhary


Comments

Author: nazarml01 Nov 2008 Member Level: Silver   Points : 2

hi Madam.

please solve my problem...
i have upload file from Asp.Net into "C:/UploadFile/" location.

Now.
i want to download file from uploaded directory. but this directory is not server Path

if u have any code pls send me

this is my messengers ID
Yahoo nazar_ml@yahoo.com
Google nazarmlcs@gmail.com

Author: Mrs. Meetu Choudhary Nanda01 Nov 2008 Member Level: Gold   Points : 1

Nazarml

where you have have saved the file the location which you have given is from where u took the file but where u saved it and from where you want to download it plese tell me so that i can help you

++
Thnaks and Regards
Meetu Choudhary

Author: nazarml02 Nov 2008 Member Level: Silver   Points : 1

dear madam Meetu Choudhary
lot of thanks for your response

when i upload file i saved into "C:\upload_File\"
i want to download file from above path.

How can do?
but.
I save asp project in "D:\Asp\upload-File\"

pls help me.

Regard
Nazarml



Author: Mrs. Meetu Choudhary Nanda25 Mar 2010 Member Level: Gold   Points : 1

in that case what you need to do is that save the fserver location in your webconfig file and then instead of server.mappath use that file location from the web config file.

Author: Jayesh L Lolariya09 Apr 2010 Member Level: Silver   Points : 1

hi Experts ,
i am looking for solution for my Query

my Query is i want to save image in to the Excell workbook and
give me solution it is urgent for me because bases on that task i need to work on so other issue


Author: Pal (Parthiv) Patel29 Jul 2010 Member Level: Gold   Points : 0

Thanks Mam its nice code.

Keep It !!

Author: Pal (Parthiv) Patel11 Aug 2010 Member Level: Gold   Points : 1

Hey mam
one query is for you .
Yor above code work perfectly but one problem is ther when i try to downlod file like
"web docu.pdf" means space between words that file not download perfectly.
Plese see this matter.

Author: Pavan14 Oct 2010 Member Level: Silver   Points : 1

HI all,
My requirement is a bit related to performance. currently it was consuming huge time.

i had a huge data in MSSQL. i need to download it and show in client machine with some formatting also like backcolor, widths, etc.

my question is if i had 1lakh of records. how can i achieve the performance optimally? need suggestions...

Regards,
Pa1

Author: Dhinesh Muthuvel16 Oct 2010 Member Level: Silver   Points : 1

For downloading the file from server to client

WebClient class has a DownloadFile method which would be useful.

For more information refer to link mentioned below.
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx

Guest Author: enas02 Sep 2012

thanks alot this code works fine

Guest Author: Howard Taylor13 Oct 2014

I have the same code but my SaveAs() dialog tries to save the file as the ASPXpageName.aspx. If I change the extension it saves the file as an Excel file. Good BUT why doesn't it know to name the file correctly? Thanks.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: