JSFiddle - React, Tailwind, and code Playground
JavaScript
var url = '//enable-cors.org/img/cloud-download.svg';
var password = '123456';
function download (url, password, cb) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
// xhr.setRequestHeader('PASSWORD_HEADER', password);
xhr.onload = function () {
cb(xhr.response);
};
xhr.send(null);
}
download(url, password, function (arrayBuffer) {
var file = new File([arrayBuffer], 'some filename');
var a = document.createElement('A');
a.setAttribute('href', window.URL.createObjectURL(file));
a.setAttribute('download', 'file-name-of-download.ext');
// in firefox `a.click()` works only if `a` element is in DOM, so...
document.documentElement.appendChild(a);
a.click();
console.log('done');
});