본문 바로가기

공부/Vue.js

axios 사용한 excel 파일 다운로드 요청

파라미터의 responseType: 'blob' 를 사용하지 않으면, 엑셀파일이 정상적으로 열리지 않는다.

GET 방식


downCsList(){                
    axios({
        method: 'GET',
        url: 'http://localhost:9099/list/testDownload',                 
        responseType: 'blob' // 가장 중요함
    })    
    .then(response =>{        
        const url = window.URL.createObjectURL(new Blob([response.data], { type: response.headers['content-type'] }));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'test.xlsx');
        document.body.appendChild(link);
        link.click();
    })                                
}

POST 방식

axios({
    method: 'POST',
    url: 'http://localhost:9099/list/download',
    responseType: 'blob',
    headers: {
        "Content-Type": "application/json"
    },   
    data: {
        custCode: custCodeVal,
        startTime: startTimeVal,
        endTime: endTimeVal       
    } 
})
.then(response =>{
    const url = window.URL.createObjectURL(new Blob([response.data], { type: response.headers['content-type'] }));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'test.xlsx');
    document.body.appendChild(link);
    link.click();
})

참고 :

'공부 > Vue.js' 카테고리의 다른 글

Vue PWA 빌드 에러 : Cannot find module 'chalk'  (0) 2019.03.04
Vue 프로젝트 npm 패키지  (0) 2019.02.10
Vue CLI 웹팩  (0) 2019.02.10