axios和ajax对响应是文件流用blob处理


先看axios请求处理,下载文件

this.$axios.get(api.exportMortgageOrderExcelVisit, { params: params, responseType: 'blob'})
            .then(res => {
              let url = window.URL.createObjectURL(new Blob([res]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          let excelName = '下载文件.xlsx'
          link.setAttribute('download', excelName)
          document.body.appendChild(link)
          link.click()
            })
            .catch(() => {
              
            })

ajax请求,文件转换成图片(使用原生ajax,因为jquery没有blob数据格式)

     var xhr = null;
        if(window.XMLHttpRequest) {
          xhr = new XMLHttpRequest();
        } else {
          xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
xhr.open(
"GET",$apiClent.getLoginImgCheckCode,true); xhr.responseType = "blob"; xhr.send(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ console.log(xhr.getResponseHeader('codeNum')) var imgUrl = window.URL.createObjectURL(new Blob([xhr.response])) $('#verification-img').attr('src', imgUrl) } }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM