我目前正在开发Ionic应用程序,并停留在文件下载部分。我看到许多文章认为FileTransfer
Cordova库现在已弃用,以支持XHR请求。
即使我看到很多关于该库已弃用的帖子,也找不到任何示例代码(用于从URL下载文件)。
任何人都可以建议我不使用FileTransfer
插件从url下载文件的好方法吗?
您可以按照以下步骤实现:
步骤1:从URL下载的下载功能
downloadFile(path: string, body: Object = {}): Observable<any> {
let headers = {} // add authentication headers and other headers as per your requirement
return this.http.post/get(
`${path}`, body, { headers: headers, withCredentials: true }
)
.catch((err) =>console.log(err))
.map((res:Response) => res)
.finally( () => { });
}
步骤2:使用下载功能将其转换为适当的Blob。
this.downloadFile(`url`, postData).subscribe(
res => {
let options = { type: ‘filetype’ };
let filename = ‘filename.type’;
Util.createAndDownloadBlobFile(res._body, options, filename);
},
err => {
// show the error
}
);
第3步:使用以下插件https://github.com/apache/cordova-plugin-file将Blob数据保存在设备上
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句