使用 AJAX 调用输出存储为 BLOB 的 PDF 文件内容,无需创建文件
我需要为点击下载按钮的用户打开一个 pdf 文件。
下载按钮对 servlet 进行 ajax 调用,该 servlet 从包含 PDF 内容的数据库中的 blob 字段获取数据,并将其作为响应返回。
我该怎么做才能将响应作为 PDF 文件下载给用户。
servlet 代码如下:
response.setContentType("application/pdf");
oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
byte[] bytes = blob.getBytes(1, (int) blob.length());
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
当我检查响应的 Fire bug 时,我收到一个很长的响应,其中包含 AJAX 中的以下字符。
�a�J��㔎��ji�2K���y�2��q F�f�9�G��!%�kɂ��W��������mp) {h̕�S���NJ_�A����'����2k��j���яR�>wB�e�|=w�p�%w��qǦ>�~�1o �㾙9j�B�;aNx3�`z����c�O����$�;�N|;�x���;�-�c�f�M�c���(�f �M6K��
�a�J��㔎��ji�2K���y�2��q F�f�9�G��!%�kɂ��W��������mp){h̕� S���_�A����'����2k��j���яR�>wB�e�|=w�p�%w��qǦ>�~�1o�㾙9j �B�;aNx3�`z���c�O����$�;�N|;�x���;�-�c�f�M��c�(�f�M6K� 我不想提交页面或弹出 servlet 窗口,其中包含为 URL 中显示的查询发送的参数
我也不想在服务器上创建该文件。
有没有办法获取ajax调用中servlet的响应并将其显示到页面或Iframe,然后浏览器自动下载文件...
I need to open a pdf file for the user who clicks on a download button.
The download button makes a ajax call to a servlet which gets data from a blob field in the database containing PDF contents, and returns it as a response.
What can I do in order to make the response to be downloaded as a PDF file to the user.
The servlet code is as below:
response.setContentType("application/pdf");
oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
byte[] bytes = blob.getBytes(1, (int) blob.length());
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
I receive a long response containing characters like the ones below in AJAX when I checked the Fire bug for the response.
�a�J��㔎��ji�2K���y�2��q F�f�9�G��!%�kɂ��W��������mp){h̕�S���NJ_�A����'����2k��j���яR�>wB�e�|=w�p�%w��qǦ>�~�1o�㾙9j�B�;aNx3�`z��طc�O��ï��$�;�N|;�xۇ��;�-�c�f�M��c���(�f�M6K���
I don't want to submit the page or to popup a window for the servlet with the parameters sent for the query showing in the URL
I also don't want to create the file on the server.
Is there a way to take the response of servlet coming in ajax call and display it to a page or Iframe and the browser automatically downloads the file...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法使用 Ajax 下载文件。然而,使用隐藏的 IFRAME 可以实现相同的“效果”。不使用 XMLHttpRequest,而是动态创建隐藏的 iframe 并使用正确的参数将其提交到 servlet。当响应到来时,浏览器将根据内容类型/扩展名自动处理内容。如果您使用的是 jQuery,那么此插件内置了此功能。
Its is NOT possible to download a file using Ajax. However, the same 'effect' can be achieved using a hidden IFRAME. Instead of using XMLHttpRequest, dynamically create a hidden iframe and submit it to the servlet with the proper parameters. When the response comes, the browser will automatically handle the content based on the content-type/extension. If you are using jQuery, then this plugin has this functionality in-built.
听起来 Rahulmohan 知道他的东西,但可能值得尝试将文件名标头添加到响应流中。我在 asp.net 中做了类似的事情,但不是在 Ajax 环境中,我的代码和你的代码之间的唯一区别是这一行:
Response.AddHeader("Content-Disposition", "attachment; filename=" & Filename & "。 pdf")
但我的页面不能在 Ajax 环境中运行。
Sounds like Rahulmohan knows his stuff but it might be worth trying to add a filename header to your response stream. I do something similar in asp.net but not in an Ajax environment and the only difference between my code and yours is this line:
Response.AddHeader("Content-Disposition", "attachment; filename=" & Filename & ".pdf")
My page doesn't run in an Ajax environment though.