当前位置 : 主页 > 网络编程 > JavaScript >

ajax get请求 下载文件

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt exportExl(url,name){ axios.get(url).then(res = { const BOM = '\uFEFF'; // Add BOM to text for open in excel correctly if (window.Blob window.URL window.URL.createObjectURL) { const csvData = new Blob([BOM + res.data], { type:
gistfile1.txt
exportExl(url,name){
                axios.get(url).then(res => {
                    const BOM = '\uFEFF';
                    // Add BOM to text for open in excel correctly
                    if (window.Blob && window.URL && window.URL.createObjectURL) {
                        const csvData = new Blob([BOM + res.data], { type: 'text/xls' });
                        const link = document.createElement('a');
                        link.download = name;
                        link.href = URL.createObjectURL(csvData);
                        link.target = '_blank';
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    } else {
                        const link = document.createElement('a');
                        link.download =name;
                        link.href = 'data:attachment/xls;charset=utf-8,' + BOM + encodeURIComponent(res.data);
                        link.target = '_blank';
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    }
                }).catch(err =>{
                });
            },
网友评论