Phonegap iOS 从网上下载文件到本地文件系统不工作

Phonegap iOS Download files from online to local file system not working

使用 iOS 平台的 phonegap 构建,下载程序无法运行! 有人能告诉我为什么下面的代码返回错误消息:

("download error source " + error.source); 

无法从网上下载图片 // 例如:http://www.sushikoapp.com/img/branches/thumbs/big_sushiko-bchamoun.jpg

("upload error code" + error.code);  sometimes 1 and sometimes 3

代码如下:

var ios_directoryEntry = fileSystem.root;
ios_directoryEntry.getDirectory("branches", { create: true, exclusive: false }, onDirectorySuccessiOS, onDirectoryFailiOS);
var ios_rootdir = fileSystem.root;
//var ios_fp = ios_rootdir.fullPath;
var ios_fp = ios_rootdir.toURL();
ios_fp = ios_fp + "branches/" ;

//ios_fp = "cdvfile://localhost/persistent/branches/";

var fileTransfer = new FileTransfer(); 
fileTransfer.download(encodeURI(imgURL + "branches/thumbs/big_sushiko-bchamoun.jpg" ), ios_fp + "big_big_sushiko-bchamoun.jpg",
                    function (entry) {
                        alert("download complete: " + entry.fullPath); 
                        }
                    },
                 function (error) {
                     //Download abort errors or download failed errors
                     alert("download error source " + error.source);
                     alert("upload error code" + error.code);
                 }
        );  

感谢您的建议...

您正在尝试将文件写入 iOS 根文件系统,这是不可能的,因为 iOS 中的所有应用程序都是沙盒并且只能访问它们自己的沙盒。

所以不要使用 fileSystem.root,而是 fileSystem.documents

问题是您将文件 API 操作视为同步操作,而实际上它们是异步的。 您可以使用 cordova.file 来引用文件系统上的目标文件夹(参见此处:https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md)。

所以尝试这样的事情:

resolveLocalFileSystemURL(cordova.file.documentsDirectory, onGetDocuments, function(error) {
    console.error("Error getting Documents directory: "+error.code);
});

function onGetDocuments(entry) {
    console.log("Got Documents directory");
    entry.getDirectory("branches", { create: true, exclusive: false }, onGetBranches, function(error) {
        console.error("Error creating/getting branches directory: "+error.code);
    });
}

function onGetBranches(entry){
    console.log("Got branches directory");
    doFileTransfer(entry.toURL());
}

function doFileTransfer(ios_fp){
    var fileTransfer = new FileTransfer(); 
    fileTransfer.download(encodeURI(imgURL + "branches/thumbs/big_sushiko-bchamoun.jpg" ), ios_fp + "big_big_sushiko-bchamoun.jpg",
        function (entry) {
            alert("download complete: " + entry.fullPath); 
            }
        },
         function (error) {
             //Download abort errors or download failed errors
             alert("download error source " + error.source);
             alert("upload error code" + error.code);
        }
    );
}