본문 바로가기

Vue

브라우저 blob 데이터 받기

blob type vnd.ms-excel 데이터 받기

 

ie는 ‘msSaveOrOpenBlob’라는 파일을 만들고 다운로드하기위한 자체 API가 있어서 그걸써야한다.

 

this.$http.instance().post(url, domain, {responseType:'blob',headers: {"Content-Type": "application/json"}}).then((response)=>{
    console.log("성공");
    const filename = util.replaceAll(decodeURI(response.headers['content-disposition'].split("filename=")[1]), '"', '');

    // ie 10+
    if(window.navigator.msSaveBlob) {
        const blob = new Blob([response.data]);
        window.navigator.msSaveBlob(blob, filename);
    } else { // not ie
        const blob = window.URL.createObjectURL(new Blob([response.data], { type: 'vnd.ms-excel' })); // ms-excel 데이터 받을거임
        const link = document.createElement('a');
        link.href = blob;
        link.setAttribute('download', filename);
        document.body.appendChild(link);
        link.click();
    }
}).catch(err=>{
    console.log("excel 실패",err);
});

'Vue' 카테고리의 다른 글

[vue] ngrok 을 이용해서 외부에서 로컬로 접속하기  (0) 2022.05.17
eslint, prettier  (0) 2021.12.14
같은 라이브러리 다른버전 npm install  (0) 2021.10.05
[Vue3] Watch, WatchEffect  (0) 2021.09.06
Vue Cli 3 IE polyfill 설정하기  (0) 2020.06.25