jquery ajax请求pdf文件流下载显示空白解决方案

$.ajax({
                cache: true,
                type: "GET",
                url:'https://gjtool/pdfh5/git.pdf',  //pdf文件流的请求接口
                async: false,
                mimeType: 'text/plain; charset=x-user-defined',//jq ajax请求文件流的方式  (起作用的重点)
                error: function () {
                    alert("网络原因请求失败!");
                },
                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';
                    if('download' in document.createElement('a')){
                        //非IE下载
                        const elink = document.createElement('a');
                        elink.download=fileName;
                        elink.style.display='none';
                        elink.href = URL.createObjectURL(blob);
                        document.body.appendChild(elink);
                        elink.click();
                        URL.revokeObjectURL(elink.href);
                        document.body.removeChild(elink);
                    }else {
                        navigator.msSaveBlob(blob,fileName);
                    }

                }
  });

更多推荐

jquery ajax请求pdf文件流下载显示空白解决方案