从服务器端 asp.NET 动态设置图像控件图像

发布于 2024-11-01 09:54:54 字数 731 浏览 1 评论 0原文

我有一个网络表单网站,用户上传文件,文件名等保存到数据库中。然后这些文件将显示在数据列表中。我试图让这个数据列表显示不同的图像(图标)来表示文件类型。

这是我后面的代码。 fm.getIcon 是一个自定义函数,它将服务器上的完整文件路径返回到表示文件类型的适当图像。

当我调试代码时,我可以验证图像确实存在于 imgFile 路径中

Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound

        Dim docName As Label = e.Item.FindControl("fileNameLabel")
        Dim docImage As Image = e.Item.FindControl("image1")
        Dim imgFile As String = fm.getIcon(My.Computer.FileSystem.GetFileInfo(docName.Text).Extension)
        docImage.ImageUrl = imgFile

End Sub

我的问题是图像未加载。如果我用图像的硬编码路径替换 imgFile ,它就可以正常工作。

我缺少什么?

I have a webforms site where users upload files, the file name etc is saved to DB. These files are then displayed in a datalist. I'm trying to get this datalist to show different images (icons) to represent the file types.

This is my code behind. fm.getIcon is a custom function that returns the full file path on the server to the approppriate image which represents the file type.

When i debug the code i can verify that the image does exist at the imgFile path

Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound

        Dim docName As Label = e.Item.FindControl("fileNameLabel")
        Dim docImage As Image = e.Item.FindControl("image1")
        Dim imgFile As String = fm.getIcon(My.Computer.FileSystem.GetFileInfo(docName.Text).Extension)
        docImage.ImageUrl = imgFile

End Sub

My problem is that the image is not loading. If i replace imgFile with a hardcoded path to an image it works fine.

What am i missing ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

妄想挽回 2024-11-08 09:54:54

请尝试这个。

// 第一种方法

string fileLocation = Server.MapPath("~/Images/MyFile.jpg");

FileInfo fileInfo = new FileInfo(fileLocation);

string fileExtension = fileInfo.Extension;

// 第二种方法

System.IO.Path.GetExtension(Server.MapPath("~/Images/MyFile.jpg")); // Result: .jpg

// 你的获取图标方法

docImage.ImageUrl = "~/Icons/" + 
fm.getIcon(Path.GetExtension(Server.MapPath("~/Images/MyFile.jpg")));

public string getIcon(string extension)
{
  switch (extension.ToLower())
  {
    case ".asa":
      return "asa.png";
    case ".asax":
      return "asax.png";
    case ".ascx":
      return "ascx.png";                
    default:
      return "unknown.png";
  }
}

Please try this.

// 1st Method

string fileLocation = Server.MapPath("~/Images/MyFile.jpg");

FileInfo fileInfo = new FileInfo(fileLocation);

string fileExtension = fileInfo.Extension;

// 2nd Method

System.IO.Path.GetExtension(Server.MapPath("~/Images/MyFile.jpg")); // Result: .jpg

// Your get icon method

docImage.ImageUrl = "~/Icons/" + 
fm.getIcon(Path.GetExtension(Server.MapPath("~/Images/MyFile.jpg")));

public string getIcon(string extension)
{
  switch (extension.ToLower())
  {
    case ".asa":
      return "asa.png";
    case ".asax":
      return "asax.png";
    case ".ascx":
      return "ascx.png";                
    default:
      return "unknown.png";
  }
}
热风软妹 2024-11-08 09:54:54

您应该确认 fm.getIcon 实际上返回了图标图像的有效 URL,而不是机器级文件路径。您应该能够获取 fm.getIcon 的输出并将其粘贴到浏览器中并查看图像。文件权限可能是一个问题,为网站提供服务的 IIS 进程需要能够读取图标图像文件。

You should confirm that fm.getIcon is actually returning a valid URL for the icon image, not a machine-level file path. You should be able to take the output of fm.getIcon and paste it into a browser and see the image. File permissions could be a issue, the IIS process serving the website needs to be able read the icon image files.

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