Phonegap iOS将文件从在线下载到本地文件系统不起作用

时间:2015-02-06 15:21:04

标签: ios cordova download

使用适用于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);
                 }
        );  

感谢您的建议......

2 个答案:

答案 0 :(得分:0)

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

所以不要使用fileSystem.rootfileSystem.documents

答案 1 :(得分:0)

问题是你将文件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);
        }
    );
}