.NET Core API-从服务器而不是从本地存储返回图像URL

问题描述

在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();

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...