[英]Unable to download blob as file in Angular 7 from API
I've been having some trouble getting file download working in my application. 我在使用我的应用程序进行文件下载时遇到了一些麻烦。 To download the file I made a new call in the API I am using to get data into the application.
为了下载文件,我在API中进行了一次新的调用,用于将数据导入应用程序。 I've tested this API using Postman and it does seem to be working as I am able to download files with the call.
我已经使用Postman测试了这个API,它似乎正在工作,因为我能够通过调用下载文件。
Unfortunately I am running into some issues with implementing it into my Angular application. 不幸的是,我遇到了将其实现到我的Angular应用程序中的一些问题。 When I call the function below I am getting a 'corrupt' file as it is unable to be opened.
当我调用下面的函数时,我收到一个“损坏”的文件,因为它无法打开。 I have checked other questions/solutions related to my problem but after trying most of them I am getting no further.
我已经检查了与我的问题相关的其他问题/解决方案,但在尝试了大部分问题后,我没有进一步。
The call in my service: 我的服务电话:
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)
}
Using the call of the service: 使用服务的调用:
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);
});
I hope someone can help me out as I am not seeing what is wrong with this part of my code. 我希望有人可以帮助我,因为我没有看到我的代码的这部分出了什么问题。
In DownloadFile()
the parameters are: 在
DownloadFile()
,参数是:
companyId, fileId, extension, fileName
but when you call the service it looks like you have flipped the filename and extension parameters to: 但是,当您调用该服务时,您似乎已将文件名和扩展名参数翻转为:
companyId, fileId, fileName, extension
Easy mistake to make. 容易犯错误。 :)
:)
Happy coding. 快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.