jquery ajax 异步请求 pdf
GA666666
2021-04-23 AM
39℃
0条
$.ajax({
//chtype=pdf&style=horizontal
url: 'UploadServlet?style='+resstyle+'&chtype='+reschtype,
type: 'POST',
data: data,
async: true,
cache: false,
contentType: false,
processData: false,
mimeType: 'text/plain; charset=x-user-defined',//jq ajax请求文件流的方式 (起作用的重点)
success: function (data) {
var rawLength = data.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (i = 0; i < rawLength; i++) {
array[i] = data.charCodeAt(i) & 0xff;
}
//上面是把后台请求到的文件流进行转化为符合的流
var blob = new Blob([array],{type: 'application/pdf;charset-UTF-8'});
var fileName = 'PDF'+new Date().getTime()+'.pdf';
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download=fileName;
link.click();
window.open(link);
clearAll();
closeModal();
},
error: function (returndata) {
alert(returndata.responseText ) // 可以替换为自己的方法
}
});