I'm having some problems downloading files with JS. I have files in Firebase storage and I need to download them. I will not use firebase storage download codes.
This file download URL is as below.
https://firebasestorage.googleapis.com/...
What I want is for it to download the download from the browser as soon as I say download. or when I say download, the download will start and I need to show the percentage of the download to the user.
First, I couldn't find anything to start the download in the browser. By browser I mean it starts the download from the downloads section of the browser.
Secondly, I had problems with downloading in the background and displaying the percentage. I tried a few things for this.
There is a library called js-file-downloader. It meets what I want, but when the download starts I can't get the percentage and when the download is finished the file opens in a new window. It should download the file to the device. https://github.com/AleeeKoi/js-file-downloader
<script src="js/downloader/dist/js-file-downloader.min.js"></script>
function download() {
var url = "https://firebasestorage.googleapis.com/...";
new jsFileDownloader({
url: url,
process: process,
filename: "a.jpg"
})
.then(function () {
console.log("Successfull");
})
.catch(function (error) {
console.log("ERROR");
});
}
function process (event) {
if (!event.lengthComputable) return;
var downloadingPercentage = Math.floor(event.loaded / event.total * 100);
console.log(downloadingPercentage)
};
- I am downloading the file using AJAX. The only problem here is that I can't get the download percentage.
function download() {
var url = "https://firebasestorage.googleapis.com/...";
$.ajax({
url: url,
method: 'GET',
xhrFields: {
responseType: 'blob'
},
progress: function(e) {
if (e.lengthComputable) {
const completedPercentage = Math.round((e.loaded * 100) / e.total);
console.log(completedPercentage);
}
},
success: function (data) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = "a.png";
document.body.append(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
});
}
I would be very happy if you could help me with this subject. Thank you from now. If there is any missing information, I will add it when you tell me.