文章来源地址https://www.toymoban.com/news/detail-796923.html
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
<iframe style="width: 100%;height: 500px" :src="path"></iframe>

文章来源:https://www.toymoban.com/news/detail-796923.html
到了这里,关于【Vue】后端返回文件流,前端预览文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!