javascript
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
html
<iframe style="width: 100%;height: 500px" :src="path"></iframe>
文件预览效果