【问题标题】:Downloading a file with Cordova使用 Cordova 下载文件
【发布时间】:2013-05-22 16:25:39
【问题描述】:

尽管在文档或此论坛中找到了许多示例,但我找不到使用 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);
        }
    );

我在控制台中得到这个错误:Uncaught TypeError:

Cannot call method 'getFile' of undefined

uri在config.xml中被授权。

【问题讨论】:

    标签: android cordova download filesystems


    【解决方案1】:

    我认为问题出在这一行: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的值。它是一个可能必须创建的目录。所以我遵循了documentation中给出的另一个例子。
    • 我明白了;一开始我误会了。我不得不删除额外的getFile 函数。但是,我注意到新目录是在 SD 卡中创建的。没有办法使用应用目录吗?
    • 您好@Yako 要回答您的问题,您可以使用Cordova 提供的FileSystem 对象,并使用属性fileSystem.files,您可以使用应用程序文件目录。更多信息在这里:github.com/apache/cordova-plugin-file#android
    【解决方案2】:
    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=="
            }
        }
    );
    

    我使用它但成功并下载已完成但在你的应用程序中找不到图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 2021-05-27
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多