我正在使用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目录,但以上内容均未写入该目录。这仅仅是模拟器错误还是我做错了什么?

谢谢!

最佳答案

我在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);


}

查看屏幕截图-

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

10-11 22:53
查看更多