下载后文件出错
我有一个Word文档,它可以在服务器上完美打开,但是当我使用我网站的按钮单击事件下载它时,它就会崩溃。 我正在使用下面的代码单击按钮来下载文档。 请帮助解决这个问题: 我正在使用.net框架3.5
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=StandardLetter" + _responseId.ToString() + ".doc");
Response.ContentType = "application/octet-stream";
Response.TransmitFile (Server.MapPath("~/document/letter/StandardLetter" + _responseId.ToString() + ".doc"));
I have word document which is opening perfectly at server but when i download it using button click event of my website it gets currept.
i am using below code on button click to make document download.
please help to resolve this problem:
i am using .net framework 3.5
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=StandardLetter" + _responseId.ToString() + ".doc");
Response.ContentType = "application/octet-stream";
Response.TransmitFile (Server.MapPath("~/document/letter/StandardLetter" + _responseId.ToString() + ".doc"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您发布的代码后面有
Response.End()
吗?如果没有,您将从添加到传输文件的 aspx 文件中获得额外的“html”代码 - 从而损坏它。编辑
正如 Akshay Anand 提到的,更好的方法是调用
HttpContext.Current.ApplicationInstance.CompleteRequest();
而不是Response.End()
请参阅文档。另请参阅此问题。Do you have a
Response.End()
after that code you posted? If not, you will get extra "html" code from the aspx file added to the transmitted file - thus corrupting it.EDIT
As Akshay Anand mentioned, a better way would be to call
HttpContext.Current.ApplicationInstance.CompleteRequest();
instead ofResponse.End()
see docs. See also this question.好吧,这是我使用的代码,它是 vb,但很容易转换;)
这适用于 PDF,并且通过将 .ContentType 更改为 Excel 也可以将其吐出。所以我假设这将采用任何 MIME 类型。祝你好运!
我将名为 MergedFile 的 pdf 文档转换为 byte(),然后给它一个可由用户输入的“ShortName”。内容长度非常重要..
Ok well here is the code I use, it's vb but easily converted ;)
This works for PDF and by changing .ContentType to Excel spits that out too.. So I assume this will take any MIME type. Good luck!
I take my pdf document called MergedFile and convert it to a byte(), I give it a 'ShortName' that can be entered by the user. Content-Length is very important..
相反,请尝试:
我不使用 Word,但对于 Excel,我使用:
Instead try:
I dont use Word but for Excel I use: