asp教程.net word文档下载代码
//第一种方法:
string path = server.mappath("文件名.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=文件名.doc");
response.flush();
response.binarywrite(filedata);
response.end();
//第二种方法:
解决下载doc文件时自动在页面打开的问题:
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();
调用:
<asp:hyperlink id="hyperlink1"
runat="server"
navigateurl='<%#string.format("x.ashx?filename={0}",getfilepath(eval("fileurl").tostring()))%>'> <%#eval("filename")%>
</asp:hyperlink>