C# .NET核心API-从服务器而不是本地存储返回图像URL
在Chrome中,我遇到以下错误:C# .NET核心API-从服务器而不是本地存储返回图像URL,c#,.net,asp.net-apicontroller,C#,.net,Asp.net Apicontroller,在Chrome中,我遇到以下错误: Not allowed to load local resource: C://... 现在我想更改像'localhost:44346/wwwroot/images/profileimages/img.jpg'中的图像返回 你能告诉我怎么做吗 这是我的文件上载控制器: [HttpPut] [Authorize] [RequestSizeLimit(1_000_000)] [Route("UpdateImage&quo
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;
}
}
[HttpPut]
[授权]
[RequestSizeLimit(1_000_000)]
[路线(“更新图像”)]
公共异步任务更新页面([FromForm]ApplicationUserModel模型)
{
尝试
{
var user=await\u userManager.FindByIdAsync(model.id);
if(user.ProfileImagePath!=null)
{
var file=newfileinfo(user.ProfileImagePath);
Delete();
}
var uniqueFileName=GetUniqueFileName(model.ProfileImage.FileName);
var uploads=Path.Combine(hostingEnvironment.WebRootPath,“Images\\ProfileImages”);
var filePath=Path.Combine(上传,uniqueFileName);
等待model.ProfileImage.CopyToAsync(新文件流(filePath,FileMode.Create));
user.ProfileImagePath=文件路径;
var result=await\u userManager.UpdateAsync(用户);
返回Ok(结果);
}
捕获(例外情况除外)
{
掷骰子;
}
}
这是用于获取用户信息的控制器:
[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;
}
}
[HttpGet]
[授权]
[路由(“GetCurrentUser”)]
公共异步任务GetCurrentUser()
{
尝试
{
var userId=User.FindFirst(“userId”)?.Value;
var data=await\u userManager.FindByIdAsync(userId);
var user=newuserstandardmodel
{
id=用户id,
LastName=data.LastName,
FirstName=data.FirstName,
ProfileImagePath=data.ProfileImagePath
};
返回Ok(用户);
}
捕获(例外情况除外)
{
掷骰子;
}
}
所以我找到了解决问题的方法。
当我触发GetCurrentUser函数时,我将检查文件路径是否以“C”开头。
如果这是真的,我将用我的本地主机地址格式化文件路径。就像布鲁诺建议的那样
但这只有在Startup.cs中有UseStaticFiles()时才有效
app.UseStaticFiles();
这并不漂亮,但它可以完成工作,而且只用于测试
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
};
if (user.ProfileImagePath.StartsWith('C'))
{
var url = "https://localhost:44356/";
user.ProfileImagePath = user.ProfileImagePath.Remove(0,58);
user.ProfileImagePath = url + user.ProfileImagePath;
}
return Ok(user);
简单一点,filePath=ApplicationHostAddress+$“/wwwroot/images/profileimages/{imageName}”。你知道你的主机名,你知道你的图像将在哪里,只是连接他们!!!hostingEnvironment.WebRootPath的价值是什么