ARTICLE

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

Posted by Krishnan LN Articles | Web Development 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
post comment
     

thnx

Posted by vipan sharma May 30, 2012

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
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter