gpt4 book ai didi

ios - Phonegap 和 iOS 模拟器 - 无法保存下载的文件

转载 作者:行者123 更新时间:2023-11-29 02:06:43 24 4
gpt4 key购买 nike

我正在使用 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 目录,但上面无法写入它。这只是模拟器错误还是我做错了什么?

谢谢!

最佳答案

我在文档目录下创建了一个名为 dummy.html 的文件。下面是下载文件的工作代码片段。您可以记录路径并查看其指向的位置。使用safari开发工具iOS模拟器进行检查。我添加了文件和文件传输插件。

function downloadFile() {
window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {
create: true,
exclusive: false
},
function gotFileEntry(fileEntry) {
console.log(fileEntry);
var sPath = fileEntry.nativeURL.replace("dummy.html", "");
console.log(sPath);
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download(
"http://developer.android.com/assets/images/home/ics-android.png",
sPath + "dummy.png",
function(theFile) {
console.log("download complete: " + theFile.toURI());
showLink(theFile.toURI());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
}
);
},
fail);
},
fail);

查看截图-

enter image description here

关于ios - Phonegap 和 iOS 模拟器 - 无法保存下载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com