首页 > 解决方案 > Azure blob 容器中的图像未显示在 ASP.net 页面上的 ListView 中

问题描述

我正在Listview从 sql server 数据库加载一个。行包含一个blob ID. 我blob ID从 blob 容器的基本 url 构造 s 然后尝试显示图像。什么都没发生。这是我的代码:

protected void PhotosListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var itm = e.Item;
    ImageButton imgBtn = (ImageButton)itm.FindControl("itemImageButton");
    Label TNlbl = (Label)itm.FindControl("BlobPhotoIDLabel");
    string blobContainerId = (string)Session["BlobContId"];
    string imgUrl = blobClient.StorageUri.PrimaryUri + blobContainerId.ToLower() + "/" + TNlbl.Text;
    imgBtn.ResolveClientUrl(imgUrl);
}

这是标记:

<td runat="server" style="background-color: #DCDCDC; color: #000000;">
                <asp:ImageButton ID="itemImageButton" runat="server" 
                            CommandArgument="<%# Container.DataItem %>" 
                            Width="180" Height="120"
                            OnCommand="itemImageButton_Command"/>
                <br />PhotoId:
                <asp:Label ID="PhotoIdLabel" runat="server" Text='<%# Eval("PhotoId") %>' />
                <br />PhotoTitle:
                <asp:Label ID="PhotoTitleLabel" runat="server" Text='<%# Eval("PhotoTitle") %>' />
        // etc

它的见鬼的是另一个页面上的类似代码工作正常。我错过了什么?我也在数据绑定和数据绑定事件中尝试过。数据库中的标题和其他数据显示正常。构造的 Blob url 似乎有效。

标签: asp.netazurelistviewazure-blob-storage

解决方案


构造的 Blob url 似乎有效。

您可以尝试将他的 blob url 放在浏览器中以检查图像是否可用。

如果您无法在浏览器中查看它,那么正如 Gaurav 所说,检查您容器的Public Access Level. 它应该是BloborContainer而不是Private(no anonymous access).

如果 url 有效,则使用以下代码显示图像:

Url=imgBtn.ResolveClientUrl(imgUrl); //Gets a URL that can be used by the browser.
imgBtn.imageUrl(Url);

更多详情ResolveClientUrl,可以参考这篇文章


推荐阅读