1

Provide Download manual button. On click of these button a pdf file more than 5 MB should be downloaded. A progressbar should be display to show percentage of file downloaded. When download complete the progress bar should reach 100% and than it should be hidden. Popup message “Download Complete” should be display. Using JQuery.

Viral Vanani
  • 105
  • 1
  • 1
  • 10
  • 1
    Possible duplicate of [Show a progress bar for downloading files using XHR2/AJAX](https://stackoverflow.com/questions/39589917/show-a-progress-bar-for-downloading-files-using-xhr2-ajax) – Nirali Apr 14 '18 at 04:56
  • @Nirali I want progress bar with percentage – Viral Vanani Apr 14 '18 at 05:15

2 Answers2

4
 <script type="text/javascript">
    $(".download").click(function(){
    window.location.href = 'file/30mb.pdf'; //set your file url which want to download
    var data = [];
    for (var i = 0; i < 100000; i++) {
    var tmp = [];
    for (var i = 0; i < 100000; i++) {
    tmp[i] = 'hue';
    }
    data[i] = tmp;
    };
    $.ajax({
    xhr: function () {
    var xhr = new window.XMLHttpRequest();
    xhr.upload.addEventListener("progress", function (evt) {
    if (evt.lengthComputable) {
    var percentComplete = evt.loaded / evt.total;
    //console.log(percentComplete);
    $('.progress').css({
    width: percentComplete * 100 + '%'
    });
    if (percentComplete === 1) {
    $('.progress').addClass('hide');
    }
    }
    }, false);
    xhr.addEventListener("progress", function (evt) {
    if (evt.lengthComputable) {
    var percentComplete = evt.loaded / evt.total;
    console.log(Math.round(percentComplete*100)+"%");
    $(".perc").text(Math.round(percentComplete*100)+"%");
    if((Math.round(percentComplete*100))==100)
    {
    $(".perc").text("download completed");
    $('.progress').removeClass('hide');
    // $(".perc").text("");
    }
    $('.progress').css({
    width: percentComplete * 100 + '%'
    });
    }
    }, false);
    return xhr;
    },
    type: 'POST',
    url: "file/30mb.pdf", //set your file url which want to download
    data: data,
    success: function (data) {}
    });
    });

    </script>

Output

https://jsfiddle.net/viralvanani/x2uhyjjL/

Viral Vanani
  • 105
  • 1
  • 1
  • 10
1

You could use a plugin like ajax-progress.

I've created a small replication on jsfiddle

const url = '//somefile.pdf'

$.ajax(url, {
  progress: function(e) {
    if (e.lengthComputable) {
      const completedPercentage = Math.round((e.loaded * 100) / e.total);
      // You can inject completedPercentage into the DOM now
      console.log(completedPercentage);
    }
  }
})

Note that the URL you put there must be reachable by your domain (ie the Access-Control-Allow-Origin should be set).

kano
  • 5,626
  • 3
  • 33
  • 48
  • What part? Does it log any errors? You gotta help me out here a bit :) Also there's many questions on SO which this is a duplicate of, sorta. Have you looked at those solutions as well? – kano Apr 17 '18 at 06:54
  • @Kano he's right it doesn't work. the part that doesn't work is the logging for the progress. Otherwise it seems to download just fine. – moeiscool Aug 06 '19 at 03:30
  • Is is possible to use this when a user downloads a file? I have a loader widget I want to hide when the file is done. This plug-in works for that, but the file is not saved on the user's machine... not sure if JS will even allow that. – splashout Oct 20 '20 at 23:51