下载后文件出错

发布于 2024-09-10 03:43:54 字数 432 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与酒说心事 2024-09-17 03:43:54

您发布的代码后面有 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 of Response.End() see docs. See also this question.

纵情客 2024-09-17 03:43:54

好吧,这是我使用的代码,它是 vb,但很容易转换;)

 Response.ContentType = "application/pdf"
        Dim byteArray As Byte() = File.ReadAllBytes(MergedFile)

        Response.AddHeader("Content-Disposition", "attachment;filename=""" & ShortFilename & """")
        Response.AddHeader("Content-Length", byteArray.Length)
        Response.BinaryWrite(byteArray)
        Response.Flush()
        Response.End()

这适用于 PDF,并且通过将 .ContentType 更改为 Excel 也可以将其吐出。所以我假设这将采用任何 MIME 类型。祝你好运!

我将名为 MergedFile 的 pdf 文档转换为 byte(),然后给它一个可由用户输入的“ShortName”。内容长度非常重要..

Ok well here is the code I use, it's vb but easily converted ;)

 Response.ContentType = "application/pdf"
        Dim byteArray As Byte() = File.ReadAllBytes(MergedFile)

        Response.AddHeader("Content-Disposition", "attachment;filename=""" & ShortFilename & """")
        Response.AddHeader("Content-Length", byteArray.Length)
        Response.BinaryWrite(byteArray)
        Response.Flush()
        Response.End()

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..

不离久伴 2024-09-17 03:43:54

相反,请尝试:

Response.ContentType ="application/msword";

我不使用 Word,但对于 Excel,我使用:

Response.ContentType = "application/x-msexcel"

Instead try:

Response.ContentType ="application/msword";

I dont use Word but for Excel I use:

Response.ContentType = "application/x-msexcel"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文