从Angular中的.NET API获取文件内容

问题描述

我正在研究Angular 7和.NET Core应用程序。我必须将.NET API中的文件文件内容)传递给Angular。我的API代码是:

public async Task<IActionResult> GetLicense@R_567_4045@ion()
{
    try
    {
        string fileName = "TestLicense";

        string filePath = PathOfFile;

        var bytearray = System.IO.File.ReadAllBytes(pathToSaveLicenseFile);
        const string contentType = "text/plain";
        var result = new FileContentResult(bytearray,contentType)
        {
            FileDownloadName = fileName
        };

        return result;
    }
    catch (Exception ex)
    {
    
    }
    return null;
}

我在Angular上的服务代码是:

getLicense@R_567_4045@ion(): Observable<any> {

return this.http.get(BaseUrl + 'getlicense@R_567_4045@ion',{responseType: 'blob',observe: 'response'});
}

我的组件代码是:

ngOnInit() {

this.uploadService.getLicense@R_567_4045@ion().subscribe(data => {
  var blob = new Blob([data.blob()],{type: 'text/plain'});
  })
}

运行代码时,出现异常 data.blob不是函数

我也尝试过 data.body(),这没有给出异常,但是我没有得到文件的数据。我已经用邮递员尝试了这个API及其工作正常,即我确实在邮递员中获取文件内容。可能是什么问题?我在做什么错了?

解决方法

您是否尝试过这种方法:https://www.blexin.com/en-US/Article/Blog/Uploading-and-Downloading-files-with-Angular-and-AspNet-Core-22