【发布时间】:2020-10-16 14:23:52
【问题描述】:
在 Chrome 中我收到以下错误:
Not allowed to load local resource: C://...
现在我想更改图像的返回值,例如 'localhost:44346/wwwroot/images/profileimages/img.jpg'
你能告诉我怎么做吗?
这是我的文件上传控制器:
[HttpPut]
[Authorize]
[RequestSizeLimit(1_000_000)]
[Route("UpdateImage")]
public async Task<IActionResult> UpdateImage([FromForm]ApplicationUserModel model)
{
try
{
var user = await _userManager.FindByIdAsync(model.id);
if (user.ProfileImagePath != null)
{
var file = new FileInfo(user.ProfileImagePath);
file.Delete();
}
var uniqueFileName = GetUniqueFileName(model.ProfileImage.FileName);
var uploads = Path.Combine(hostingEnvironment.WebRootPath, "Images\\ProfileImages");
var filePath = Path.Combine(uploads, uniqueFileName);
await model.ProfileImage.CopyToAsync(new FileStream(filePath, FileMode.Create));
user.ProfileImagePath = filePath;
var result = await _userManager.UpdateAsync(user);
return Ok(result);
}
catch (Exception ex)
{
throw ex;
}
}
这是获取用户信息的控制器:
[HttpGet]
[Authorize]
[Route("GetCurrentUser")]
public async Task<IActionResult> GetCurrentUser()
{
try
{
var userId = User.FindFirst("UserID")?.Value;
var data = await _userManager.FindByIdAsync(userId);
var user = new UserStandardModel
{
id = userId,
LastName = data.LastName,
FirstName = data.FirstName,
ProfileImagePath = data.ProfileImagePath
};
return Ok(user);
}
catch (Exception ex)
{
throw ex;
}
}
【问题讨论】:
-
简单点,filePath = ApplicationHostAddress + $"/wwwroot/images/profileimages/{imageName}"。你知道你的主机名,你知道你的图像将在哪里,只需将它们连接起来!!!
-
hostingEnvironment.WebRootPath 的值是多少
标签: c# .net asp.net-apicontroller