【Vue】后端返回文件流,前端预览文件
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
文件预览效果
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...