Jquery下的AJAX请求文件流的PDF预览
$.ajax({
url: 'http://127.0.0.1:3000/getPdf',
method: 'get',
data: {Id:id},
mimeType: 'text/plain;charset=x-user-defined',
success: function (res) {
let resLength = res.length
let arr = new Uint8Array(new ArrayBuffer(resLength))
for (let i = 0; i < resLength; i++) {
arr[i] = res.charCodeAt(i)&0xff;
}
let blob = new Blob([arr], {
type: 'application/pdf;charset=UTF-8'
})
const pdfView = window.URL.createObjectURL(blob)
window.open(pdfView)
},
error: function (err) {
console.log('失败:', err)
}
})
原生请求下的PDF文件预览
let xhr = new XMLHttpRequest();
let url = 'http://127.0.0.1:3000/getPdf?Id='+id;
xhr.open('get', url);
xhr.responseType = 'blob';
xhr.onload = function () {
if (this.status === 200) {
let blob = this.response;
const pdfView = window.URL.createObjectURL(blob);
window.open(pdfView)
}
}
xhr.send();
axios请求下的PDF预览
axios({
methods: 'post',
url: '',
responseType: 'blob',
}).then(res => {
let blob = new Blob([arr], {
type: 'application/pdf;charset=UTF-8'
})
const pdfView = window.URL.createObjectURL(blob)
window.open(pdfView)
})