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

问题描述

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

public async Task<IActionResult> GetLicenseinformation()
{
    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上的服务代码是:

getLicenseinformation(): Observable<any> {

return this.http.get(BaseUrl + 'getlicenseinformation',{responseType: 'blob',observe: 'response'});
}

我的组件代码是:

ngOnInit() {

this.uploadService.getLicenseinformation().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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...