js实现pdf文件查看

ajax 实现pdf文件流预览

js部分

//
<div id="add_dialog" class="easyui-dialog" title=""
     data-options="modal:true,closed:true,resizable:true,iconCls:'icon-save'"
     style="width:750px;height:500px;">
</div>

//pdf预览
    openPdf: function (id) {
        $.ajax({
            url:url,
            type: "POST",
            dataType: "binary",
            data: {id: id},
            headers: {Authorization: Authorization},
            success: function (data, status, xhr) {
                if (data) {
                    var headers = xhr.getAllResponseHeaders();
                    var contentDisposition = xhr.getResponseHeader("Content-Disposition");
                    var fileName = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9);
                    if (fileName) {
                        fileName = decodeURIComponent(fileName);
                        const reader = new FileReader();
                        reader.readAsDataURL(data);
                        reader.onload = (e) => {
                            debugger;
                            $("#add_dialog").dialog({
                                title: '查看pdf',
                                width: 1000,
                                height: 600,
                                modal: true,
                                content: "<iframe  scrolling='auto' frameborder='0' src='" + e.target.result + "' style='width:100%; height:100%; display:block;'></iframe>"
                            });
                            $("#add_dialog").dialog("open"); // 打开dialog
                            $('#add_dialog').window('center');//使Dialog居中显示
                        };
                    }
                }
            },
            error: function (error) {
                $.messager.show({
                    title: "提示",
                    msg: "error!",
                    timeout: 2000
                });
                console.log(error);
            }
        });
    },

//jquery ajax请求二进制数据支持不完善。 dataType: “binary”,这块需要自定义xmlhttprequest方法,拓展二进制流方法

后台

@Override
    public void downLoad(HttpServletResponse res) {
        String path =	"pdf文件地址";
        try {

            File file = new File(path);
            if (file.exists()) {
                //
                FileInputStream fileInputStream = new FileInputStream(file);
                res.setHeader("Connection", "close");
                res.setHeader("Content-Type", "application/pdf;charset=UTF-8");
                res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
                BufferedInputStream bufferedInputStream = new BufferedInputStream(
                        fileInputStream);
                byte[] b = new byte[bufferedInputStream.available()];
                bufferedInputStream.read(b);
                OutputStream outputStream = res.getOutputStream();
                outputStream.write(b);
                // 人走带门
                bufferedInputStream.close();
                outputStream.flush();
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值