![](/img/trans.png)
[英]How to get different types of responseType in angular from asp.net core web API method
[英]How to receive a file from ASP.NET Core Web API from Angular service and display in a different chrome tab
我有一個 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;
}
}
它返回一個文件,如下所示:
現在我想在服務中使用 Angular 和以下 function 來使用服務:
getFile(id: number): Observable<File> {
return this.http.get<File>(
this.uri + `/file/${id}`
);
}
有人可以幫助如何使用這個 api 以及如何在 Chrome 中顯示圖像/文件?
謝謝。
您正在返回一個文件,該文件將適用於下載文件。 不要返回文件,而是將文件保存在項目中的某個位置,然后返回 angular 的路徑。 然后顯示圖像 src 的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.