Skip to content

Instantly share code, notes, and snippets.

@anamolsoman
Last active July 15, 2020 14:26
Show Gist options
  • Save anamolsoman/9b2ab3b4c7939c57a38c1054c2226678 to your computer and use it in GitHub Desktop.
Save anamolsoman/9b2ab3b4c7939c57a38c1054c2226678 to your computer and use it in GitHub Desktop.
Force File download in Vuejs
<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