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) } }

 

posted @ 2020-08-26 18:58  honely1314  阅读(1827)  评论(0编辑  收藏  举报