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