我正面临ionic3应用中的问题。
让我详细描述一下我的情况:实际上我需要离线支持我的离子应用程序。 所以每当我调用API时,我都会将数据存储到本地存储中。 并将图像从api下载到我的本地目录。 这样当我无法从本地资源获得互联网时,我可以获取数据和图像。
我使用这个插件将图像从服务器下载到本地: https : //ionicframework.com/docs/native/file-transfer/
如果我运行以下命令它工作正常:
ionic cordova run android
但是当我运行以下命令时,它无法正常工作:
ionic cordova run android --prod
代码:
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
constructor(private transfer: FileTransfer, private file: File) { }
const fileTransfer: FileTransferObject = this.transfer.create();
download() {
const url = 'http://www.example.com/file.pdf';
fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
}
我没有从控制台收到任何错误或问题。 所以我不知道我错过了什么。 还可以很好地配置本地存储。 所以许可不是问题。
最后我找到了解决这个问题的方法! 首先你应该更新这个命令:
npm i @ionic/app-scripts@latest --save
npm i ionic-native@latest --save
并且可能在代码中的某处,您之前调用与文件传输插件相关的任何内容
platform.ready.then()
在我的情况下:我注入了一些包含这样的行的服务:
this.fileTransfer = this.transfer.create();
我把它改成了这个:
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.fileTransfer = this.transfer.create();
});
现在一切正常。
更多细节 :
为什么这在调试模式下工作?
答案非常明确,因为在调试模式下设备就绪事件给出了很长时间的火灾和文件传输,这绝对称之为! 但在生产模式下,设备准备就绪非常快,并在此之前调用文件传输。 我希望这对你有帮助。