Wednesday, May 27, 2009

How to download a file from server using asp.net C#

Downloading a file from server is a common task for most of the applications. Such as client wants to offer free download, free newletter etc. And i am sure that when you read this post you will get another articles with source code download facility. Am i right? Which means its our common task. Here i will try to show you how we can write the code to download a server file into our client or local PC.

Focus Area:
1. Download a file
2. Reusability
3. Security aspects
4. File size limitations
5. Few common errors
6. Important links


Download a file:
Let we have a folder in our virtual directory named download. Now from this folder i want to download a file named XMLFile.xml. To do that add a new aspx page. Drag & drop a button control on this page. Now under the button click event write the below code:

protected void cmdDownload_Click(object sender, EventArgs e)
{
Response.ContentType =
"APPLICATION/OCTET-STREAM";
String Header = "Attachment; Filename=XMLFile.xml";
Response.AppendHeader(
"Content-Disposition", Header);
System.IO.
FileInfo Dfile = new System.IO.FileInfo(Server.MapPath("download/XMLFile.xml"));
Response.WriteFile(Dfile.FullName);
//Don't forget to add the following line
Response.End();
}

Thats it. Now you can download your required file from the server.

Reusability:
Lets you have to provide this option in 5 pages then does it viable for you to write same code in 5 pages. There a lot of way for reusabilty but here i will show you the simplest way to achieve this. Add a class & modify this in the following way:

public static class Downloader
{
public static void Download(string sFileName,string sFilePath)
{
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String Header = "Attachment; Filename="+sFileName;
HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
System.IO.
FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
HttpContext.Current.Response.WriteFile(Dfile.FullName);
HttpContext.Current.Response.End();
}
}

Now you can reuse the method from any pages. The below code segment show you how you can call this method from your aspx page.

protected void cmdDownload_Click(object sender, EventArgs e)
{
Downloader.Download("XMLFile.xml", "download/XMLFile.xml");
}

In my article on Upload file i have discussed Security aspects & file size. To read Click Here.

Few common errors:
1. Could not find a part of the path........
Solution: Make sure that you have specified the path correctly.

2. Access or permission denied...
Solution: Read Security aspects & file size from above link.

3. HTML text/code added in the last with my downlaoded file.
Solution: You missed to add Response.End(); in your code.

Important Links :
http://www.west-wind.com/weblog/posts/76293.aspx
http://www.devarticles.com/c/a/ASP.NET/HTTP-File-Download-Without-User-Interaction-Using-.NET/1/
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q316431

2 comments:

Anonymous said...

i have this error...

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

Anonymous said...

good article

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash