【Vue】后端返回文件流,前端预览文件

 【Vue】后端返回文件流,前端预览文件_第1张图片

let date;
        request({
          url: this.$route.query.url,
          method: 'get',
          responseType: 'blob',
        }).then(resp => {
          date = resp
          this.path = window.URL.createObjectURL(new Blob([resp], {type: "application/pdf"}))
        }).catch((e) => {
          //旧版本浏览器下的blob创建对象
          window.BlobBuilder = window.BlobBuilder ||
            window.WebKitBlobBuilder ||
            window.MozBlobBuilder ||
            window.MSBlobBuilder;
          if (e.name == 'TypeError' && window.BlobBuilder) {
            if (date) {
              BlobBuilder.append(date);
              this.path = URL.createObjectURL(new BlobBuilder().getBlob("application/pdf"))
            }
          } else {
            console.log("浏览器版本较低,暂不支持该文件类型预览");
          }
        }).finally(() => {
          window.URL.revokeObjectURL(this.path);
        })
responseType必须设置为blob
  

【Vue】后端返回文件流,前端预览文件_第2张图片 文件预览效果

你可能感兴趣的:(【前端篇】,前端,vue.js,javascript)