Reader Level:
Articles

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

By Krishnan LN on February 28, 2006
Again, this is for beginners to have a method of downloading and reading word documents from server.
  • 0
  • 0
  • 98458
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.");

          }

}

Krishnan LN

Krishnan L. N. is working in Cognizant, Chennai, INDIA. He has 7 years experience working with Microsoft .NET technologies.

COMMENT USING

Trending up