文件流下载
后端返回的文件流
注意:responseType应设置为:’arraybuffer’,这样返回的文件流才会是二进制的,才能使用new Blob得到正确的文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| this.$axios .post(url接口地址, params请求参数, { headers: { token: token }, responseType: "arraybuffer" }) .then((file) => { let content = file.data; let a= document.createElement("a"); a.download = "附件.zip"; a.style.display = "none"; let blob = new Blob([content], {type: "application/zip"}) a.href = URL.createObjectURL(blob); document.body.appendChild(a); a.click(); document.body.removeChild(a); })
|
==下载Excel ==>type: “application/vnd.ms-excel”
原生js
1 2 3 4 5 6 7
| let a = document.createElement('a'); let blob = new Blob([response.data], {type: "application/vnd.ms-excel"}); let objectUrl = URL.createObjectURL(blob); a.setAttribute("href",objectUrl); a.setAttribute("download", '计划单电子表格.xls'); a.click();
|
原生js使用new Blob()实现带格式导出excel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table id="table" border="1"> <tr style="height:50px;"> <th style="width:100px;color:red;">姓名</th> <th>性别</th> <th>年龄</th> </tr> <tr> <td>小明</td> <td>男</td> <td>16</td> </tr> <tr> <td>小红</td> <td>女</td> <td>17</td> </tr> <tr> <td>小张</td> <td>男</td> <td>17</td> </tr> </table> <a id="down">点击下载excel</a> <script> let html = document.getElementById("table").outerHTML; let blob = new Blob([html],{ type: 'application/vnd.ms-excel'}); let a = document.getElementById('down'); a.href = URL.createObjectURL(blob); a.download = '测试excel下载' </script> </body> </html>
|
vue导出下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| async downLoad() { await request({ url: 'xxxxxx/getExcel', method: 'get', responseType: 'blob' }) .then(res => { const blob = new Blob([res.data], { type: 'application/octet-stream' }); if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, '订单列表.xls'); } else { const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = '作品列表.xls'; link.click(); window.URL.revokeObjectURL(link.href); } this.getData(); }); }
|
上传==>Content-Type: multipart/form-data;
1.使用form表单提交
1 2 3 4 5
| <form action="请求地址" method="请求类型" enctype="multipart/form-data"> <input type="file" name=""> <input type="text" name=""> <input type="submit" value="提交"> </form>
|
2.使用编程式FormData对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| var file = e.target.files[0]; var form = new FormData(); form.append("file", file); form.append("fileName", file.name); form.append("errorType", "1"); form.append('webmasterfile',oBlob); var xhr = new XMLHttpRequest(); var url= "http://localhost:8080/upload.do"; xhr.open("POST", url); xhr.send(form); xhr.onreadystatechange = function(){ if(xhr.readyState==4 && xhr.status==200){ var resultObj = JSON.parse(xhr.responseText); } } uploadAjax(form).then(({ data: res }) => { console.log(res); });
|
使用formData表单上传时axios会默认设置 headers: { ‘Content-Type’: ‘multipart/form-data’ }