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

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.");

          }

}


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles