Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js code for webpage download progress bar

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="a1" data-filename="filename.xml">Click to download</div>
<script>
$('#a1').click(function() {
    var that = this;
    var page_url = 'download.php';

    var req = new XMLHttpRequest();
    req.open("POST", page_url, true);
    req.addEventListener("progress", function (evt) {
        if(evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            console.log(percentComplete);
        }
    }, false);

    req.responseType = "blob";
    req.onreadystatechange = function () {
        if (req.readyState === 4 && req.status === 200) {
            var filename = $(that).data('filename');
            if (typeof window.chrome !== 'undefined') {
                // Chrome version
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(req.response);
                link.download = filename;
                link.click();
            } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
                // IE version
                var blob = new Blob([req.response], { type: 'application/force-download' });
                window.navigator.msSaveBlob(blob, filename);
            } else {
                // Firefox version
                var file = new File([req.response], filename, { type: 'application/force-download' });
                window.open(URL.createObjectURL(file));
            }
        }
    };
    req.send();
});
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript if equal infinity 
Javascript :: use navigation in class component react native drawer navigation 
Javascript :: react native image swiper 
Javascript :: alert in react native 
Javascript :: discord js channel send 
Javascript :: width and height with node js 
Javascript :: an array of functions 
Javascript :: js array join 
Javascript :: jsonl parser 
Javascript :: javascript access map elements 
Javascript :: how to compile typescript to javascript es6 
Javascript :: Getting One Value from an Array of Items 
Javascript :: react throttle render 
Javascript :: buffer concat nodejs 
Javascript :: react navigation 4 
Javascript :: how to move an element to the cursor in javascript 
Javascript :: split function in javascript 
Javascript :: run only one test cypress 
Javascript :: padstart in javascript 
Javascript :: javascript filter array 
Javascript :: array.filter 
Javascript :: for in and for of in js 
Javascript :: react-dropzone-uploader 
Javascript :: copy an array 
Javascript :: how to upgrade nodejs version 
Javascript :: how to declare variables javascript 
Javascript :: get last item in array js 
Javascript :: find in javascript 
Javascript :: redux workflow 
Javascript :: append css file with javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =