简体   繁体   English

如何从 ASP.NET 内核 Web API 接收文件并在不同的 ZC31C335EF37283C451B18BA0DD31 服务选项卡中显示

[英]How to receive a file from ASP.NET Core Web API from Angular service and display in a different chrome tab

I have a Web API written in .NET Core which returns a file as follows:我有一个 Web API 写在 .NET Core 中,它返回如下文件:

[Authorize]
[HttpGet("{id}")]
public async Task<IActionResult> GetFile(int id)
{
    var response = await _fileService.GetFile(id);

    Response.Clear();

    return File(response.FileByteArray, response.FileType, response.FileName);
}

public async Task<FileResponseDTO> GetFile(int fileId)
{
    using (var unitOfWork = _unitOfWorkFactory.CreateUnitOfWork())
    {
        var response = new FileResponseDTO();
        var file = await unitOfWork.FileRepository.GetFile(fileId);

        if (file != null)
        {
            string filePath = GetFilePath(file.FileName);

            response.FileByteArray = System.IO.File.ReadAllBytes(filePath);
            response.FileName = file.FileName;
            response.FileType = file.FileType; ;
        }

        return response;
    }
}

It returns a file as follows:它返回一个文件,如下所示:

在此处输入图像描述

Now I want to consume the service using Angular with the following function in a service:现在我想在服务中使用 Angular 和以下 function 来使用服务:

getFile(id: number): Observable<File> {
    return this.http.get<File>(
        this.uri + `/file/${id}`
    );
}

Can someone please help how to consume this api and also how can I display the image/file in Chrome?有人可以帮助如何使用这个 api 以及如何在 Chrome 中显示图像/文件?

Thanks.谢谢。

You are returning a file, which will work for the downloading file.您正在返回一个文件,该文件将适用于下载文件。 Instead of returning file, save the file a location on your project and return the path to angular.不要返回文件,而是将文件保存在项目中的某个位置,然后返回 angular 的路径。 And then show the path to your image src.然后显示图像 src 的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从asp.net核心Web API方法获取角度的不同类型的responseType - How to get different types of responseType in angular from asp.net core web API method 如何将文件从 Angular 上传到 ASP.NET Core Web API - How to Upload File from Angular to ASP.NET Core Web API 无法将值从angular服务发布到asp.net core angular中的Web API - Unable to post values from angular service to Web API in asp.net core angular 将文件从 angular 11 形式传递到 asp.net 核心 5 web Z8A5DA552ED12654E72Z3 的最佳方式是什么 - What is the best way pass file from angular 11 form to asp.net core 5 web api 从angular上传文件到asp.net核心web api controller - Upload file from angular to asp.net core web api controller 尝试将文件从 Angular 6 发布到 ASP.NET Core Web Api 时出错 - Error on try to post file from Angular 6 to ASP.NET Core Web Api 文件无法从 Angular 上传到 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D083A1 - File failed to upload from Angular to ASP.NET Core Web API 如何使用ASP.NET CORE Web API从Angular 6发送电子邮件? - How Can I send an Email from Angular 6 with ASP.NET CORE web api? 如何从 Angular 中的 ASP.Net Core Web API 请求令牌 - How do I request a token from a ASP.Net Core Web API in Angular 如何从 Angular 中的本地 Web API (ASP.NET Core) 获取数据 - How to get data from local Web API (ASP.NET Core) in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM