问题描述
我正在研究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.body(),这没有给出异常,但是我没有得到文件的数据。我已经用邮递员尝试了这个API及其工作正常,即我确实在邮递员中获取了文件内容。可能是什么问题?我在做什么错了?