Last active
July 15, 2020 14:26
-
-
Save anamolsoman/9b2ab3b4c7939c57a38c1054c2226678 to your computer and use it in GitHub Desktop.
Force File download in Vuejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import axios from 'axios' | |
export default { | |
data() { | |
return { | |
filesrcs: [ | |
{ | |
title: 'AadharCard.png', | |
src: require('~/assets/image/home/demo1.jpg'), | |
}, | |
{ title: 'PANCard.png', src: require('~/assets/image/home/demo1.jpg') }, | |
{ title: 'Licence.png', src: require('~/assets/image/home/demo1.jpg') }, | |
{ title: 'Photo.png', src: require('~/assets/image/home/demo1.jpg') }, | |
], | |
} | |
}, | |
methods: { | |
forceFileDownload(response, title) { | |
console.log(title) | |
const url = window.URL.createObjectURL(new Blob([response.data])) | |
const link = document.createElement('a') | |
link.href = url | |
link.setAttribute('download', title) | |
document.body.appendChild(link) | |
link.click() | |
}, | |
downloadWithAxios(url, title) { | |
axios({ | |
method: 'get', | |
url, | |
responseType: 'arraybuffer', | |
}) | |
.then((response) => { | |
this.forceFileDownload(response, title) | |
}) | |
.catch(() => console.log('error occured')) | |
}, | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment