版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wtf4007/article/details/84842347
由于ie9不支持blob对象,通过ajax下载文件没戏。
解决方案:通过模拟表单提交,下载文件,兼容ie9。
话不多说,直接上代码(代码结合了业务,填入了token。没此要求的去掉即可)
import {loginUser} from '../user'; const addParam = (formElement,param) =>{ Object.keys(param).forEach(key => { let inputElement = document.createElement("input"); inputElement.type = 'hidden'; inputElement.name = key; inputElement.value = param[key]; formElement.appendChild(inputElement); }) } export default (method,path, data, callback) => { let formElement = document.createElement('form'); formElement.style.display = 'display: none'; formElement.method = method; formElement.action = path; formElement.target = 'callBackTarget'; if(Array.isArray(data)){ data.forEach(param => addParam(formElement,param) ) }else{ addParam(formElement,data); } const user = loginUser(); if(user){ let inputElement = document.createElement("input"); inputElement.type = 'hidden'; inputElement.name = 'token'; inputElement.value = user.token; formElement.appendChild(inputElement); } document.body.appendChild(formElement); formElement.submit(); document.body.removeChild(formElement); callback && callback(); };