Download blob with progress indicator

This commit is contained in:
Joel Collins 2019-11-29 10:58:30 +00:00
parent 1c9363a026
commit 2844735f6d

View file

@ -1,13 +1,13 @@
<template> <template>
<div v-if="zipBuilderUri"> <div v-if="zipBuilderUri" class="uk-width-small">
<div v-show="downloadReady"> <div v-show="downloadReady">
<button <button
:disabled="isDownloading" :disabled="isDownloading"
type="button" type="button"
class="uk-button uk-button-default uk-form-small" class="uk-button uk-button-default uk-form-small uk-width-1-1"
@click="downloadWithAxios()" @click="downloadWithAxios()"
> >
{{ isDownloading ? "Downloading..." : "Download" }} {{ isDownloading ? `${downloadProgress}%` : "Download" }}
</button> </button>
</div> </div>
<div v-show="!downloadReady"> <div v-show="!downloadReady">
@ -47,6 +47,7 @@ export default {
data: function() { data: function() {
return { return {
downloadProgress: 0,
isDownloading: false, isDownloading: false,
downloadReady: false, downloadReady: false,
downloadUrl: null, downloadUrl: null,
@ -92,6 +93,9 @@ export default {
}, },
forceFileDownload(response) { forceFileDownload(response) {
// Start file download by creating and clicking an invisible link
// One day I hope for a better solution to exist for this...
// A man can dream.....
const url = window.URL.createObjectURL(new Blob([response.data])); const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement("a"); const link = document.createElement("a");
link.href = url; link.href = url;
@ -101,9 +105,22 @@ export default {
}, },
downloadWithAxios() { downloadWithAxios() {
// Configure progress indicator and response type
let config = {
responseType: "blob",
onDownloadProgress: progressEvent => {
this.downloadProgress = Math.floor(
(progressEvent.loaded * 100) / progressEvent.total
);
}
};
// Set downloading flag
this.isDownloading = true; this.isDownloading = true;
// Start download
axios axios
.get(this.downloadUrl, { responseType: "arraybuffer" }) .get(this.downloadUrl, config)
.then(response => { .then(response => {
this.forceFileDownload(response); this.forceFileDownload(response);
this.deleteLastZip(); this.deleteLastZip();