使用Cordova下载文件

3

尽管在文档或论坛中可以找到许多例子,但我无法找到使用Cordova下载文件的方法...

首先,获取rootFS:

function gotFS(fileSystem) {
    console.log("got filesystem");
    // save the file system for later access
    console.log(fileSystem.root.fullPath);
        // displays "/" on desktop
        // displays "file:///mnt/sdcard" on android with SD Card
    window.rootFS = fileSystem.root;
}

document.addEventListener('deviceready', function() {                
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function(){
        console.log("error getting LocalFileSystem");
    });
}, false);

上传脚本:
// Creating the image directory
imageDir = rootFS.getDirectory("imagesContent", {create: true},
    function(){
        // Success
    },
    function(error){
        console.log("ERROR getDirectory");
        console.log(error);
    }
);

// Creating and Downloading the image
    imgFile = imageDir.getFile(filename, {create: true, exclusive: true},
        function (){     
            var localPath = rootFS.fullPath+'/imagesContent/'+filename;
            fileTransfer = new FileTransfer();        
            fileTransfer.download('http://example.com/images/'+filename,
                localPath,
                function(entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);
                    console.log('download error: ' + error.code);
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                }
            );
        },
        function (error){
            console.log("ERROR getFile");
            console.log(error);
        }
    );

我在控制台中看到了以下错误信息:未捕获的类型错误。
Cannot call method 'getFile' of undefined

在 config.xml 中授权了该 URI。

2个回答

2
我认为问题出在这一行代码上:imageDir = rootFS.getDirectory("imagesContent", {create: true},function(), function()) 我不认为调用getDirectory会返回目录名称,就像你期望的那样,这就是为什么你的imageDir对象未定义的原因。
对于从服务器下载内容,使用FileTransfer.download()方法可能更容易: http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileTransfer
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.download(
    uri,
    filePath,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
    },
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }
);

这是我尝试做的事情。但是我需要配置filePath的值。它是一个目录,可能需要创建。所以我遵循了文档中提供的另一个示例。 - Yako
我明白了,一开始我误解了。我必须删除多余的 getFile 函数。然而,我注意到新目录是在 SD 卡中创建的。有没有办法使用应用程序目录? - Yako
嗨@Yako 回答你的问题,你可以使用Cordova提供的FileSystem对象,并使用属性fileSystem.files,你可以使用应用程序文件目录。更多信息请参见: https://github.com/apache/cordova-plugin-file#android - jarodsmk

0
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.download(
    uri,
    filePath,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
    },
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }
);

我使用了它,但是成功并下载完成,但在我的应用程序中找不到图像。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接