我目前正在开发 Ionic 应用程序,并停留在文件下载部分。我看到很多帖子都说FileTransfer
cordova 库现已弃用,取而代之的是 XHR 请求。
尽管我看到很多帖子说该库已被弃用,但我找不到任何示例代码(用于从 URL 下载文件)。
谁能建议我从 url 下载文件而不使用的好方法FileTransfer
plugin?
从其他服务器下载文件可能会引起烦人CORS错误大多超出我们的控制范围。最安全的方法是绕过 Webview 并本地下载文件。您可以使用原生 HTTP plugin
在 Ionic 4 或更高版本中使用将如下所示:
import { Component } from '@angular/core';
import { HTTP } from '@ionic-native/http/ngx';
import { File } from '@ionic-native/file/ngx';
@Component({
selector: 'app-home',
templateUrl: './you-file.html',
styleUrls: ['./your-file.scss'],
})
export class HomePage {
constructor(private nativeHTTP: HTTP, private file: File) {}
private downloadFileAndStore() {
//
const filePath = this.file.dataDirectory + fileName;
// for iOS use this.file.documentsDirectory
this.nativeHTTP.downloadFile('your-url', {}, {}, filePath).then(response => {
// prints 200
console.log('success block...', response);
}).catch(err => {
// prints 403
console.log('error block ... ', err.status);
// prints Permission denied
console.log('error block ... ', err.error);
})
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)