Asp tutorial. net Word Document Download Code
// Method 1:
String path = server. mappath ("file name .doc ");
Fileinfo file = new fileinfo (path );
Filestream myfilestream = new filestream (path, filemode. open, fileaccess. read );
Byte [] filedata = new byte [file. length];
Myfilestream. read (filedata, 0, (int) (file. length ));
Myfilestream. close ();
Response. clear ();
Response. contenttype = "application/msword ";
Response. addheader ("content-disposition", "attachment?filename=file name .doc ");
Response. flush ();
Response. binarywrite (filedata );
Response. end ();
// Method 2:
Solve the Problem of automatically opening the page when downloading the doc file:
Context. response. contenttype = "application/x-octet-stream ";
String filepath = context. server. mappath (context. request. querystring ["filename"]. tostring ());
String filename = httputility. urlencode (system. io. path. getfilename (filepath). tostring (), system. text. encoding. utf8 );
Context. response. addheader ("content-disposition", "attachment; filename =" + filename );
Context. response. writefile (filepath );
Context. response. flush ();
Call:
<Asp: hyperlink id = "hyperlink1"
Runat = "server"
Navigateurl = '<% # string. format ("x. ashx? Filename = {0} ", getfilepath (eval (" fileurl "). tostring () %> '> <% # eval (" filename ") %>
</Asp: hyperlink>