问题描述
在Chrome中,出现以下错误:
Not allowed to load local resource: C://...
现在,我想更改“'localhost:44346 / wwwroot / images / profileimages / img.jpg'”中Image的返回值
你能告诉我我该怎么做吗?
这是我用于文件上传的控制器:
[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;
}
}
解决方法
所以我找到了解决我问题的方法。 当我触发我的GetCurrentUser函数时,我将检查i FilePath以“ C”开头。 如果是这样,我将使用本地主机地址格式化文件路径。像布鲁诺建议的那样。
但这仅在我的Startup.cs中具有UseStaticFiles()时有效
Query query = new Query(Criteria.where("deleted").is(false));
query.with(Sort.by(Direction.DESC,"subcolelction.order"));
List<Category> list = mongo.find(query,Category.class,"category")
这不是很漂亮,但它可以完成工作并且仅用于测试。
app.UseStaticFiles();