Download blob with progress indicator
This commit is contained in:
parent
1c9363a026
commit
2844735f6d
1 changed files with 21 additions and 4 deletions
|
|
@ -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();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue