微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐