从服务器端 asp.NET 动态设置图像控件图像
我有一个网络表单网站,用户上传文件,文件名等保存到数据库中。然后这些文件将显示在数据列表中。我试图让这个数据列表显示不同的图像(图标)来表示文件类型。
这是我后面的代码。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试这个。
// 第一种方法
// 第二种方法
// 你的获取图标方法
Please try this.
// 1st Method
// 2nd Method
// Your get icon method
您应该确认
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 offm.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.