[英]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.