如何在 asp.net core razor 中显示来自数据库文件路径的图像
How to display image from database filepath in asp.net core razor
如标题所述。我在 wwwroot 目录中有一个专门的目录。后端最初存储 C: 驱动器的整个路径,然后我将其更改为根目录。我是否应该根据解决方案资源管理器目录存储路径?如果我在解决方案资源管理器中使用完整路径,即来自 C: Drive 或来自 wwwroot 的路径,我会得到同样的错误。
上传逻辑
public async Task<IActionResult> OnPostAsync(IFormFile file)
{
if (!ModelState.IsValid)
{
return Page();
}
var UserDb = await _context.User.FindAsync(User.ID);
string folderName = "wwwroot/Uploads/Profile/" + UserDb.ID;
string webRootPath = _hostEnvironment.ContentRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
string fileName = UserDb.ID + ".jpg";
string fullPath = Path.Combine(newPath, fileName);
Console.WriteLine(fullPath);
string envpath = folderName + "/" + fileName;
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
stream.Flush();
}
UserDb.ImageUrl = folderName;
_context.Attach(UserDb).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(User.ID))
{
return NotFound();
}
else
{
throw;
}
}
var currentID = UserDb.ID;
return RedirectToPage("Socials", new { id = currentID });
}
private bool UserExists(int id)
{
return _context.User.Any(e => e.ID == id);
}
}
正在尝试显示图像
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.ImageUrl)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.ImageUrl)
<img src="@Url.Content(@Model.User.ImageUrl)" />
</dd>
解决方案资源管理器
您的 @Model.User.ImageUrl
应该等于 /Uploads/Profile/1018/1018.jpg
。请务必在开头添加 单斜线 并 指定文件名 .
因此,您可能需要像下面这样更改后端代码:
string folderName = "Uploads/Profile/" + UserDb.ID; //"Uploads/Profile/2017"
//change here...
string webRootPath = _hostEnvironment.WebRootPath; //"C:\YourSolutionLocation\wwwroot"
string newPath = Path.Combine(webRootPath, folderName);//"C:\YourSolutionLocation\wwwroot\Uploads/Profile/2017"
//....
string envpath = folderName + "/" + fileName; //"Uploads/Profile/2017/2017.jpg"
//....
UserDb.ImageUrl = envpath;
然后像下面这样更改剃刀页面:
<img src="/@Url.Content(@Model.User.ImageUrl)" />
如标题所述。我在 wwwroot 目录中有一个专门的目录。后端最初存储 C: 驱动器的整个路径,然后我将其更改为根目录。我是否应该根据解决方案资源管理器目录存储路径?如果我在解决方案资源管理器中使用完整路径,即来自 C: Drive 或来自 wwwroot 的路径,我会得到同样的错误。
上传逻辑
public async Task<IActionResult> OnPostAsync(IFormFile file)
{
if (!ModelState.IsValid)
{
return Page();
}
var UserDb = await _context.User.FindAsync(User.ID);
string folderName = "wwwroot/Uploads/Profile/" + UserDb.ID;
string webRootPath = _hostEnvironment.ContentRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
string fileName = UserDb.ID + ".jpg";
string fullPath = Path.Combine(newPath, fileName);
Console.WriteLine(fullPath);
string envpath = folderName + "/" + fileName;
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
stream.Flush();
}
UserDb.ImageUrl = folderName;
_context.Attach(UserDb).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(User.ID))
{
return NotFound();
}
else
{
throw;
}
}
var currentID = UserDb.ID;
return RedirectToPage("Socials", new { id = currentID });
}
private bool UserExists(int id)
{
return _context.User.Any(e => e.ID == id);
}
}
正在尝试显示图像
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.ImageUrl)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.ImageUrl)
<img src="@Url.Content(@Model.User.ImageUrl)" />
</dd>
解决方案资源管理器
您的 @Model.User.ImageUrl
应该等于 /Uploads/Profile/1018/1018.jpg
。请务必在开头添加 单斜线 并 指定文件名 .
因此,您可能需要像下面这样更改后端代码:
string folderName = "Uploads/Profile/" + UserDb.ID; //"Uploads/Profile/2017"
//change here...
string webRootPath = _hostEnvironment.WebRootPath; //"C:\YourSolutionLocation\wwwroot"
string newPath = Path.Combine(webRootPath, folderName);//"C:\YourSolutionLocation\wwwroot\Uploads/Profile/2017"
//....
string envpath = folderName + "/" + fileName; //"Uploads/Profile/2017/2017.jpg"
//....
UserDb.ImageUrl = envpath;
然后像下面这样更改剃刀页面:
<img src="/@Url.Content(@Model.User.ImageUrl)" />