zoukankan      html  css  js  c++  java
  • 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) } }
  • 相关阅读:
    那些不能错过的Xcode插件
    iOS开发过程中使用Core Data应避免的十个错误
    define和typedef
    #号运算符
    第三方移动后端服务开发
    9款优秀的APP免费测试框架
    网络协议初探(二)
    iphone第三方库
    HTTP状态码
    JDK8新特性:在JDK8中,默认添加final
  • 原文地址:https://www.cnblogs.com/mk2016/p/13566998.html
Copyright © 2011-2022 走看看