如何解决从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及其工作正常,即我确实在邮递员中获取了文件内容。可能是什么问题?我在做什么错了?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。