ajax get请求 下载文件

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 =>{
                });
            },

你可能感兴趣的:(web,开发笔记,javascript)