1. 程式人生 > >檔案匯出的get和post方式

檔案匯出的get和post方式

get方式(3種寫法):

1、window.open("**")是用新視窗開啟URL頁面
2、location.href="**"是用當前頁面顯示URL

window.open(this.GLOBAL.API_Spm_result+ "completionday/batchId/postId/export?batchId="+this.batchId+"&postId="+this.postId + '&access_token=' + sessionStorage.getItem('cookieaccess_token')) 
window.location.href = this.GLOBAL.API_ACCOUNT + 'users/user/export?deptId=' + exportid + '&access_token=' + sessionStorage.getItem('cookieaccess_token')
 download() {
            //下載
            var url = this.GLOBAL.API_ACCOUNT + 'users/download/user'
            this.loading1 = true  //下載中的動畫提示
            this.$get(url).then(res => {
                if (res.data.retCode) {
                    alert(res.data.message)
                } else {
                    window.location.href = url + '?access_token=' + sessionStorage.getItem('cookieaccess_token')

                }
                this.loading1 = false//下載中的動畫提示隱藏
            })
        },

post方式(這裡有兩種方式    註釋的是一種   js-file-download外掛是一種的)

outputCompound(){
                var url = this.GLOBAL.API_indicator + "indicator/composite/export"
                    var params = {
                        batchId:this.batchId,             
                        objectType:"5"
                    }               
                    axios.post(url,params,{responseType: 'arraybuffer'}).then((res) => {
                        // if(res.data.retCode){
                        //   alert(res.data.message)                    
                        // }else{
                        //     let blob = new Blob([res.data], {type: "application/vnd.ms-excel"}); 
                        //     let objectUrl = URL.createObjectURL(blob);
                        //     window.location.href = objectUrl;  
                            // 
                        //}                  
                        var fileDownload = require('js-file-download')
                        let fileName = "複合指標.csv";
                        fileDownload(res.data,fileName);
                })
            }