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

莱昂·齐默尔曼

在 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;
    }
}
莱昂·齐默尔曼

所以我找到了解决我的问题的方法。当我触发 GetCurrentUser 函数时,我会检查 FilePath 是否以“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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

.Net Core API起始URL

如何在ASP.NET Core服务器中存储外部API的访问令牌?

ASP.NET Core应用位于ssl代理服务器后面,对URL的答复为http://而不是https://

从 API 身份验证失败返回的 Azure Blob SAS Url .net core

文件夹的所有控制器的URL路由作为MVC .Net Core中的API

如何更改ASP Net Core 3 API默认启动控制器(/启动URL)

ASP.NET Core Web API如何构建URL?

ASP.NET Core中的API URL错误

从 .net core web api 中的 URL 下载大文件

在 asp.net core api 中创建回调 url

在安装了 .Net 框架的服务器上托管 .Net Core 2.2 web api

.Net Core HttpClientFactory用于多个API服务

从.Net Core API返回html文件

.NET Core API视图模型返回

如何从.Net Core Web API返回Unathorized

如何从.Net Core Web API返回Json?

从 .NET Core Web API 返回错误消息

如何在我的 .NET Core 服务器中生成 Angular 路由的 URL

.NET Core(2.1)Web API控制器接受请求URL中的所有后续内容作为参数

Picasso 未從 ASP.NET Core Web API 服務器上的 URL 加載圖像

如何在ASP.NET Core API控制器中将URL中的字符串作为参数传递

.Net Core 2.0 Web API-服务器因析构函数问题而崩溃

Asp.Net Core 2 Web API 500内部服务器错误

将 .NET Core Web API 部署到 IIS 服务器

ASP.NET Core API-500内部服务器错误,仅在生产中

ASP.NET Core Web API 500内部服务器错误

如何使用 ASP.NET Core API 连接 microsoft sql 服务器

如何在多台服务器中发布Web API Net Core 3.0

.NET Core Web API Ubuntu服务器的超级用户设置