Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Percentage bar for file downloading #965

Closed
mehrdd opened this issue Aug 9, 2013 · 2 comments
Closed

Percentage bar for file downloading #965

mehrdd opened this issue Aug 9, 2013 · 2 comments

Comments

@mehrdd
Copy link

mehrdd commented Aug 9, 2013

Hi,
Can i use node-webkit to download files with percentage ? something like google chrome download manager or some help with javascript/node ..

in this application, users have an option to download mp3 files ..
is there any way to show a percentage-bar after save-dialog ?
thank you.

@timonsku
Copy link

timonsku commented Aug 9, 2013

Depends on how you download the file but I solved it this way with a http download:

function download(file_url){
    var fs = require('fs');
    var url = require('url');
    var http = require('http');

    var options = {
        host: url.parse(file_url).host,
        port: 80,
        path: url.parse(file_url).pathname
    };

    var file_name = url.parse(file_url).pathname.split('/').pop();
    var file = fs.createWriteStream('./' + file_name);

    http.get(options, function(res) {
        var fsize = res.headers['content-length'];
        res.on('data', function(data) {
                file.write(data);
                progress(100-(((fsize-file.bytesWritten)/fsize)*100), $('#progressBar'));
            }).on('end', function() {
                file.end();
            });
        });
}
function progress(percent, $element) {
    console.log("Download: "+parseInt(percent)+" %")
    var progressBarWidth = percent * $element.width() / 100;
    $element.find('div').css( "width", progressBarWidth );
}

Hope that helps you. Gave me some headache aswell to get a proper status bar going.

@mehrdd
Copy link
Author

mehrdd commented Aug 10, 2013

Thank you very much !
i made some changes to use it with 'save file' with save dialog ..
createWriteStream ... such a nice method !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants