【发布时间】:2015-04-19 09:47:07
【问题描述】:
我正在使用 Phonegap/cordova 并编写一个 Android/iOS 应用程序,该应用程序将从我的服务器下载 json 数据并本地存储在设备上以供离线使用。在 android 上,这非常有效。我没有 iOS 设备,因此依赖于 iOS 模拟器,它会抛出“无法创建目标文件”类型错误。
downloadFile:function(path,uri){
var fileTransfer = new FileTransfer();
fileTransfer.download(
encodeURI(path),
app.getStorageLocation()+"files/"+uri,
function(entry) {
console.log("download complete: " + entry.toURL());
app.progressMove();
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false);
}
getStorageLocation 函数为:
getStorageLocation:function(){
if(device.platform == 'Android'){
return cordova.file.externalApplicationStorageDirectory;
}
else if(device.platform == 'iOS'){
return cordova.file.documentsDirectory;
}else{
throw new Error('Unsupported platform: '+device.platform);
}
}
在 iOS 模拟器上,它确实返回了 Documents 目录,但上面的内容无法写入。这只是一个模拟器错误还是我做错了什么?
谢谢!
【问题讨论】:
-
您必须在文档目录中创建要实例化的文件夹和文件
-
我该怎么做?我用了stackoverflow.com/questions/26806028/…的代码还是不行......