【发布时间】: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