我正在用离子2为android/ios制作一个应用程序,我想从网络上的一个特定网址下载一个JSON文件,然后在我的应用程序中使用它。下面是我下载的代码:
const imageLocation = `my url for .json file`;
if (this.platform.is('ios')) {
targetPath = cordova.file.applicationStorageDirectory + "data.json";
}
else if(this.platform.is('android')) {
targetPath = cordova.file.dataDirectory + "data.json";
console.log(cordova.file.dataDirectory + "data.json");
}
fileTransfer.download(imageLocation, targetPath).then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `file was successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
}, (error) => {....我希望在我的提供程序中使用这个下载的文件从它中获取数据。我怎么能这么做?
发布于 2016-12-17 17:24:20
如果要保存该文件,可以在以下多种选项中进行选择:
localStorage中或者,如果文件不是很大,可以直接将其存储在提供程序的局部变量中,即在提供程序中添加一个setter方法并将JSON对象传递给它,以便将其存储在提供程序中。请注意,在本例中,当应用程序重新启动时,数据将丢失。
https://stackoverflow.com/questions/41199631
复制相似问题