【问题标题】:Accessing Image File from Virtual Directory从虚拟目录访问图像文件
【发布时间】:2012-01-27 04:08:16
【问题描述】:

我正在尝试显示来自我网站内的虚拟目录的图像。如果我使用以下代码,图像将显示在页面上:

Dim sourcefile As String = Server.MapPath("~/Common/Images/Flag.jpg")
Response.WriteFile(sourcefile)
Response.ContentType = "image/jpg"

但如果我尝试使用以下代码在图像控件中显示图像,则图像不会显示:

Dim sourcefile As String = Server.MapPath("~/Common/Images/Flag.jpg")
Image1.ImageUrl = sourcefile

这是 HTML/ASP 代码:

    <div id="contentQE">
        <fieldset>
            <span class="graytitle">The Flag Image
                 <asp:Image ID="Image1" runat="server" />
            </span>
        </fieldset>
    </div>

我做错了什么?

【问题讨论】:

    标签: asp.net vb.net image webforms


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      使用 Image 控件中的绑定方法,我还没有测试过,但我认为它可以工作。

      Dim sourcefile As String = Server.MapPath("~/Common/Images/Flag.jpg")

      Image1.ImageUrl = 源文件

      Image1.Bind()

      【讨论】:

      • 谢谢,但显然 Bind 方法不是 Image 对象的一部分。
      • 对不起,我的意思是 DataBind() 方法。
      • 再次感谢,但仍然没有骰子。
      【解决方案3】:

      Server.MapPath 将获取文件的物理路径,这对用户从浏览器访问站点没有帮助。

      我们通常这样做:

      Dim sourcefile As String = Request.ApplicationPath & "/Common/Images/Flag.jpg"
      

      【讨论】:

      • 谢谢,但图片仍然没有显示
      • 几个问题:1) 该文件是否已经存在? 2)查看页面来源时,图片路径看起来是正确的吗?
      • 问题 1 - 是的,它与我的第一个示例使用的文件相同。问题 2 - 是的。
      • 这里是 url 的呈现方式:
      • 你试过Dim sourcefile As String = "~/Common/Images/Flag.jpg"
      猜你喜欢
      • 2015-11-19
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2013-09-26
      相关资源
      最近更新 更多