当前位置:编程学习 > C#/ASP.NET >>

asp.net Word文档下载代码

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>

 

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,