使用ASP.NET下载图片/Word文档
如果我将以下代码:
Response.ContentType = "image/jpeg"
Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg")
Response.WriteFile(MapPath("capitol.jpg"))
放入Page_Load中,我将出现下载图像的对话框。但是当我将相同的代码放入子例程中时:
Private Sub downloadPic()
MsgBox("Hello!")
Response.ContentType = "image/jpeg"
Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg")
Response.WriteFile(Server.MapPath("capitol.jpg"))
Response.End()
End Sub
我得到了 MsgBox(仅用于测试),但我无法下载图像。有什么想法吗?
If I put the following code:
Response.ContentType = "image/jpeg"
Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg")
Response.WriteFile(MapPath("capitol.jpg"))
into Page_Load, I will get the dialog box to download the image. But when I put the same code into a sub routine:
Private Sub downloadPic()
MsgBox("Hello!")
Response.ContentType = "image/jpeg"
Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg")
Response.WriteFile(Server.MapPath("capitol.jpg"))
Response.End()
End Sub
I get the MsgBox (just for testing) but I don't get the ability to download the image. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法输出到页面并在单个请求/响应中推送下载内容。
无论如何,在分配标头和
WriteFile
之前,您的代码将无法与Response.Clear()
正常工作。You can't output to the page and also push the download content within a single request/response.
Anyway your code won't work properly with a
Response.Clear()
before assigning header andWriteFile
.谢谢您的评论。我的代码的问题是我在 UpdatePanel 中有 WriteFile 代码。那是我的错误!
Thank you for the comment. The problem with my code was that I had the WriteFile code within an UpdatePanel. That was my mistake!