gpt4 book ai didi

jquery - 在 modelAndView 中嵌入 PDF

转载 作者:行者123 更新时间:2023-12-01 05:54:43 26 4
gpt4 key购买 nike

我有一个返回 PDF URL 的 WebService。问题是 CMS 很糟糕、不稳定,而且该文档甚至可能不存在。 (或者可能没有到 CMS 服务器的网络连接)

我正在使用 Spring 3.2 MVC 并且我的 Controller 足够简单:

modelAndView.addObject("documentURL", service.getCmswsGetDocumentUrl());

和 View :

<object data="${documentURL}" width="600" height="800" type='application/pdf' >

如果文档存在,这一切都可以正常工作。

但是,如果文档不存在,我希望能够执行 javascript Alert() 或其他操作。

有没有办法将pdf嵌入到modelAndView中

modleAndVIew.addObject("pdf",file);

或者我可以通过 jQuery 将数据添加到 html 对象中?

我只是不想进行 2 次调用,一次是检索文档以查看是否可以,然后一次是实际检索 html 对象标记中的文档。

有什么想法吗?

附录编辑:

我可以做这样的事情吗?: Is it possible to put binary image data into html markup and then get the image displayed as usual in any browser?

我在 Controller 中对 PDF 进行 Base64 编码并将其传递给 View ?

最佳答案

二值图像和对象标签的组合。这是 Controller :

    InputStream is = (new URL(service.getCmswsGetDocumentUrl())).openStream();
byte[] pdfBytes = IOUtils.toByteArray(is);
modelAndView.addObject("pdf",new String(Base64.encodeBase64(pdfBytes)));

和 View :

    <object  data="data:application/pdf;base64,${pdf}" width="600" height="800" type='application/pdf' >
</object>

这适用于 FF 和 Chrome,但不适用于 IE。
我有多讨厌IE?即使使用 while(1) {waysIHateIE++; 我也无法计算路数。 }

查看IE中的开发者工具,data=""有什么想法吗?

关于jquery - 在 modelAndView 中嵌入 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16900787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com