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

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

本文关键字:URL 下载 文件 中的 android 何使用 phonegap      更新时间:2023-09-26

我想从URL下载一个文件。就像如果我点击下载按钮,文件url应该得到连接,它应该下载到手机内存或android的SDCard中。

有人能帮我如何使用phonegap吗?

谢谢。

请使用Phonegap或CoreDova的FileTransfer API。以下是来自同一来源的示例代码:

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

要在SD卡上保存文件,请使用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);
}