본문 바로가기

Develop/Vue

Force file download

Force download with axios on vue.js

fileDownload = (url, method, req, fileName) => {
const axiosConfig = {
method: method,
url: url,
responseType: 'blob',
params: req,
};

try {
const response = await axios(axiosConfig);

const url = window.URL.createObjectURL(new Blob([response.data]));
const anchor = document.createElement('a');

anchor.href = url;
anchor.setAttribute('download', fileName);
document.body.appendChild(anchor);
anchor.click();
} catch (err) {
toast.error("Error on force file download process. Try again.");
}
}


'Develop > Vue' 카테고리의 다른 글

Add authorization header in ajax with axios  (0) 2018.08.07
[NUXT] error on IE browser  (1) 2018.04.05