http請求post,返回excel文件,並接收


1.post的方法里要加responseType: 'arraybuffer'參數,不然下載的excel會亂碼

2.使用{type: "application/vnd.ms-excel"}的寫法,可以保存為xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”則會保存為xlsx

3.返回結果為下載excel文檔鏈接,使用window.open(result)即可

4.使用增加節點調用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);
    })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM