简体   繁体   English

如何使用phonegap从android中的URL下载文件?

[英]How to download a file from URL in android using phonegap?

I want to download a file from URL.Like if i click on download button the file url should get connect and it should get downloaded into phone memory or SDCard of android. 我想从URL下载文件,就像单击下载按钮一样,文件url应该连接好并且应该下载到手机内存或android的SDCard中。

Can anyone please help me how to get this to work using phonegap? 谁能帮助我如何使用phonegap使其正常工作?

Thank you. 谢谢。

Please use the FileTransfer API from Phonegap or CoreDova. 请使用Phonegap或CoreDova的FileTransfer API。 Here is the sample code from the same source: 这是来自同一来源的示例代码:

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);
    }
);

To save the file on SDCard use FileWriter 要将文件保存在SDCard上,请使用FileWriter

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);  
        writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample'");
            writer.seek(4);
            writer.write(" different text");
            writer.onwriteend = function(evt){
                console.log("contents of file now 'some different text'");
            }
        };
    };
    writer.write("some sample text");
}

function fail(error) {
    console.log(error.code);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM