简体   繁体   English

通过Cordova中的FileTransfer下载文件

[英]Download file via FileTransfer in Cordova

I am developing an application for Android using Cordova. 我正在使用Cordova开发适用于Android的应用程序。 I am already familiar with FileTransfer and I already know how to download a file. 我已经熟悉FileTransfer,并且已经知道如何下载文件。 Problem is that when I am downloading a large file (20 MB) then this download tooks a while without notifying user that something is actually happening. 问题是,当我下载一个大文件(20 MB)时,此下载花费了一段时间,而没有通知用户实际正在发生的事情。

All I managed is download of file into sd card via: 我所管理的只是通过以下方式将文件下载到sd卡中:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);

and then show dialog to user that file was downloading and where (s)he can find it. 然后向用户显示该文件正在下载以及在何处可以找到该文件的对话框。 But I want let user know about progress. 但是我想让用户知道进度。 Or is it possible to somehow handle this file transfer in background so user may see a downloading icon in Android's top bar just like when downloading file via default browser? 还是可以通过某种方式在后台处理此文件传输,以便用户可以在Android顶部栏中看到下载图标,就像通过默认浏览器下载文件时一样?

Thank you very much for any help. 非常感谢您的帮助。

Code: 码:

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

function gotFS(fileSystem) {
    // save the file system for later access
    window.rootFS = fileSystem.root;
}

fileTransfer.download(
    http://somewhere.com/bigfile.zip,
    window.rootFS,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
    },
    function(error) {
        console.log("download error source " + error.source);
    },
);

Raymond Camden wrote a nice article about this. 雷蒙德·卡姆登(Raymond Camden)撰写了一篇不错的文章。 You can use bootstrap in order to show a progressbar when using the progress-function of PhoneGap in order to let a progress bar grow: 使用PhoneGap的进度功能时,可以使用引导程序来显示进度条,以使进度条增长:

fileTransfer.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
} else {
loadingStatus.increment();
}
};

Read here 在这里阅读

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

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