【问题标题】:Uploaded Image's Path is Broken in ASP.NET - unable to show Image上传的图片路径在 ASP.NET 中损坏 - 无法显示图片
【发布时间】:2019-06-22 21:24:46
【问题描述】:

之前我成功展示了这样的图片:

        <img class="centered" src="~/Content/images/gif.gif" style="max-height:12vw;" />

没问题,但这张图片已添加到解决方案的图片文件夹中。

但是,当我将图像上传到 API 并像这样存储时:

string FileName = valueobject.PictureName;
        string path = HttpContext.Current.Server.MapPath("~/UserFiles/" + valueobject.PictureOwner + "/");
        string imgPath = Path.Combine(path, FileName);

if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            File.WriteAllBytes(imgPath, valueobject.PictureImage);
            //store UserFile with path to the image
            UserFile _userFile = new UserFile();
            _userFile.FileOwner = valueobject.PictureOwner;
            string relativePath = "~/UserFiles/" + valueobject.PictureOwner + "/";
            string imgRelativePath = Path.Combine(relativePath, FileName);
            _userFile.FilePath = imgRelativePath;

图像已保存 - 文件夹已创建,图像已正确保存在 localhost 中。

但是当我尝试时,我无法解析 MVC 中的相对路径:

                        <img height="400" style="width:auto" src="@Model.HomeGalleries[g].GalleryImageStrings[i]" runat="server" />

即使这样,我也会得到“损坏”的图像图标

@Model.HomeGalleries[g].GalleryImageStrings[i] 等于 imgRelativePath

我不确定为什么这不起作用。

【问题讨论】:

    标签: c# asp.net image relative-path


    【解决方案1】:

    您必须为此实际图像路径包含 Server.MapPath。跟在这条线下面,就像一样。

    string relativePath = Server.MapPath("~/UserFiles/" + valueobject.PictureOwner + "/");
    

    请试试这个。希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2018-06-09
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多