离子文件传输插件在生产版本中不起作用
Ionic File Transfer Plugin not working in production version
我在 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();
});
现在一切正常。
更多详情:
为什么这在调试模式下工作?
答案很明确,因为在调试模式下,设备就绪事件绝对会在这之后调用很长时间来触发和文件传输!但在生产模式下,设备就绪速度非常快,文件传输在此之前调用。我希望这对你有帮助。
我在 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();
});
现在一切正常。
更多详情:
为什么这在调试模式下工作?
答案很明确,因为在调试模式下,设备就绪事件绝对会在这之后调用很长时间来触发和文件传输!但在生产模式下,设备就绪速度非常快,文件传输在此之前调用。我希望这对你有帮助。