Mik*_*123 5 api angular angular-httpclient angular7
我在使文件下载在我的应用程序中工作时遇到了一些麻烦。要下载文件,我正在使用API进行新的调用,以将数据输入到应用程序中。我已经使用Postman测试了此API,并且由于可以通过调用下载文件,因此它似乎可以正常工作。
不幸的是,在将其实现到Angular应用程序时遇到了一些问题。当我调用下面的函数时,由于无法打开,因此我得到的是“损坏”文件。我已经检查了与我的问题有关的其他问题/解决方案,但是在尝试了大多数问题/解决方案后,我没有得到进一步的解决。
我服务中的电话:
DownloadFile (companyId: string, fileId: string, extension: string, fileName: string): Observable<Blob> {
const options = { responseType: 'blob' as 'json' }
return this.http.get<Blob>(this.baseApiUrl + this.baseTag + "?companyId=" + companyId + "&fileId=" + fileId + "&extension=" + extension + "&fileName=" + fileName, options)
}
Run Code Online (Sandbox Code Playgroud)
使用服务调用:
this.data.DownloadFile(this.company.id, selectedItem.id, this.getFileNameWithoutExtension(selectedItem.fileName), this.getExtensionFromFileName(selectedItem.fileName))
.subscribe(resultBlob =>
{
//Success
console.log('start download:', resultBlob);
var blob = new Blob([resultBlob], { type: "application/pdf" } );
saveAs(blob, selectedItem.fileName);
},
error => {
//Error
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
我没有看到这段代码有什么问题。
DownloadFile()参数中有:
companyId, fileId, extension, fileName
Run Code Online (Sandbox Code Playgroud)
但是当您调用该服务时,看起来您已将文件名和扩展名参数翻转为:
companyId, fileId, fileName, extension
Run Code Online (Sandbox Code Playgroud)
很容易犯的错误。:)
| 归档时间: |
|
| 查看次数: |
6477 次 |
| 最近记录: |