SIGN UP MEMBER LOGIN:    
ARTICLE

Download Word Documents from Server Using ASP.Net and C#

Posted by Krishnan LN Articles | Web Forms C# February 28, 2006
Again, this is for beginners to have a method of downloading and reading word documents from server.
Reader Level:
Download Files:
 

In the last article that I wrote, we have seen how to read a word document present in the same system using a windows application.

Here in this article, we are going to see how to read a word file available in the server or even download it to local system.

The implementation is very simple.

Using Response.WriteFile, on giving the filename as argument when the user clicks the appropriate link in the webpage.

Code:

string filename="Connectivity.doc";

if (filename != "")

{

          string path = Server.MapPath(filename);

          System.IO.FileInfo file = new System.IO.FileInfo(path);

          if (file.Exists)

          {

                   Response.Clear();

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

                   Response.AddHeader("Content-Length", file.Length.ToString());

                   Response.ContentType = "application/octet-stream";

                   Response.WriteFile(file.FullName);

                   Response.End();

          }

          else

          {

                   Response.Write("This file does not exist.");

          }

}

Login to add your contents and source code to this article
share this article :
post comment
 

Please help me.... I am using asp.net c#. I am unable to download multiple files using IE8,but in other IE and browsers i can.Problem is only for downloading using IE8.Please help me...

Posted by arun chand Apr 21, 2012


hi

 this is bhanuji, i have one problem with Download module. My requirement is that, after download the document i should redirect to another page. Its possible or not ? i already tired like 

1. Response.Redirect("~/xyz.aspx");
2. Server.Transfer
3. Response.Write("<script type='text/javaScript'> window.location.href = '~/xyz.aspx'; </script>");

But no use...please help me its urgent..waiting for u response.

thank u

Posted by appala rao May 31, 2010

I suffer from problem but 
 when i use this code for localhost   it work fine for localhost .
But when on server it can't  work.

Please hepl me i wait for u

Posted by sanjay jagtap Jun 15, 2009

here is a nice post that explains to download files from a web server as well as from a remote web server: http://www.etechplanet.com/post/2009/02/24/Downloading-file-using-aspnet-and-C.aspx

Posted by avaneet kumar Feb 28, 2009

if i select perticular link from gridview i.e on click it should start downloading .... like save as and close will be der wt should i do? thx

Posted by sachin Feb 25, 2009
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor