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

vipan sharmaPosted May 30, 2012, 5:20 AM
thnx
arun chandPosted Apr 21, 2012, 2:20 PM
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...
appala raoPosted May 31, 2010, 2:47 AM
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
sanjay jagtapeditedPosted Jun 15, 2009, 5:45 AMEdited Jun 15, 2009, 5:48 AM
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
avaneet kumarPosted Feb 28, 2009, 6:51 AM
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
sachinPosted Feb 25, 2009, 1:20 AM
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
deepak sharmaPosted Oct 23, 2008, 3:46 PM
very good comment
nalla maheshPosted Oct 11, 2006, 6:17 AM
I saw ur code it working well for local systems/local server,but if i want to download a word documnet from other system/server (using ip address).
Elena SchulmeisterPosted Aug 15, 2006, 10:20 AM
Very good advice, but Do you have any control over an extra browser window that gets opened when things are done this way?
Nandyala sreenivasula ReddyeditedPosted Jun 5, 2006, 5:18 AMEdited Oct 10, 2006, 12:46 AM
hi I am sreenu, I have one doubt in asp.net, I want display content in IFRAME. Content is displayed in IFRAME, Main problem is only text is displayed, If any tables and image in content, it wouldn't displayed in IFRAME. It will show ASCII characters. How to remove ASCII characters. Bye sreenu