【问题标题】:How can I show files as a picture on my website?如何在我的网站上将文件显示为图片?
【发布时间】:2021-04-22 20:29:03
【问题描述】:

我有一个列表,我想在其中展示一张图片。我将“MVC/cshtml”与“.Net Core”一起使用 图片从未显示,而我给出的路径是正确的。其他图片是具有透明背景的 png 文件。

这是我得到的:

这是文件的位置:

我无法显示图片的原因是什么?

这是声明。文件名也是正确的。我可以在地图上找到它们。

@{
   string sImageFile = "~/images/artiesten/" + item.ArtiestnaamVolledig + ".jpg";
}

这是显示图片的命令。

            @if (!System.IO.File.Exists(sImageFile))
            {
                sImageFile = "~/images/artiesten/Gitaar.png";
            }
            <td class="notering_images">
                <img src="@sImageFile" alt="-" height="50" width="50" />
            </td>

【问题讨论】:

  • 我注意到您的默认图像是 .png,而您在 sImageFile 上填写 .jpg。这些是您文件的正确扩展名吗?
  • 是的,我明白了。这是8年前的代码。我正在整个环境中从头开始开发我的网站。但文件有这些扩展名。

标签: c# asp.net-core .net-core model-view-controller


【解决方案1】:

使用img标签的时候可以做个测试

<img src="~/images/artiesten/Gitaar.png" alt="-" height="50" width="50" />

图片可以正确渲染,生成的html代码如下:

<img src="/images/artiesten/Gitaar.png" alt="-" height="50" width="50">

所以,只需删除 ~

sImageFile = "/images/artiesten/Gitaar.png";

【讨论】:

  • 谢谢...只有这部分不起作用。 @{ string tempFile = System.IO.Path.Combine(ViewBag.WebRootPath, sImageFile);//.Replace("/", "\\")); if (!System.IO.File.Exists(tempFile)) { sImageFile = "/images/artiesten/Gitaar.png"; } }
  • 您对 File.Exists 部分也有正确的解决方案吗?你已经给出了我问题的正确答案。它有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
相关资源
最近更新 更多