http请求post,返回excel文件,并接收
生活随笔
收集整理的這篇文章主要介紹了
http请求post,返回excel文件,并接收
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.post的方法里要加responseType: 'arraybuffer'參數(shù),不然下載的excel會亂碼
2.使用{type: "application/vnd.ms-excel"}的寫法,可以保存為xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”則會保存為xlsx
3.返回結(jié)果為下載excel文檔鏈接,使用window.open(result)即可
4.使用增加節(jié)點調(diào)用click方法,而不使用window.open(objectUrl)方法,是防止被瀏覽器當插件屏蔽彈出連接
5.給文件設定名字,直接在a標簽的download屬性中設置即可
$http.post({url:'staffRoster/exportStaffrosterTemplate',data:sendData,responseType: 'arraybuffer'}).success(function(res){var blob = new Blob([res], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});var objectUrl = URL.createObjectURL(blob);var a = document.createElement("a");document.body.appendChild(a);a.style = "display: none";a.href = objectUrl;a.download = '員工信息表';a.click();document.body.removeChild(a);})?
更多專業(yè)前端知識,請上 【猿2048】www.mk2048.com
總結(jié)
以上是生活随笔為你收集整理的http请求post,返回excel文件,并接收的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS基础语法(三) CSS的6种特性
- 下一篇: Tomcat配置自签名https