【问题标题】:Downloading pictures/Word documents using ASP.NET使用 ASP.NET 下载图片/Word 文档
【发布时间】:2009-11-22 05:15:44
【问题描述】:

如果我输入以下代码:

    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(仅用于测试),但我无法下载图像。有什么想法吗?

【问题讨论】:

    标签: asp.net response content-type content-disposition


    【解决方案1】:

    您不能输出到页面,也不能在单个请求/响应中推送下载内容。

    无论如何,在分配标头和 WriteFile 之前,您的代码将无法与 Response.Clear() 一起正常工作。

    【讨论】:

    • 感谢您的回复。当我注释掉 Page_Load 代码并仅尝试 downloadPic() 子例程中的代码时,它不会下载。你知道有什么好的链接可以帮助我吗?
    【解决方案2】:

    感谢您的评论。我的代码的问题是我在 UpdatePanel 中有 WriteFile 代码。那是我的错!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多