简体   繁体   English

ajax回复为pdf,如何显示?

[英]ajax response as pdf, how to show it?

I have a web service that generate a pdf. 我有一个生成pdf的网络服务。 In my GAE application I have a button, when i click i use an ajax's function. 在我的GAE应用程序中,我有一个按钮,当我单击时,我使用ajax的功能。

$('#test').click(function(){
        $.ajax({
            url: 'provaws.do',
            type: 'get',
            dataType: 'html',
                    success : function(data) {
                    }
        });
    });

this is the method in java that's call ws, using UrlFetch: 这是Java中使用UrlFetch调用ws的方法:

    @RequestMapping(method = RequestMethod.GET, value = PROVAWS_URL)
public void prova(HttpServletRequest httpRequest, HttpServletResponse httpResponse, HttpSession httpSession) throws IOException{
    try {
        URL url = new URL("http://XXXXX/sap/bc/zcl_getpdf/vbeln/yyyyyy");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization","Basic " + Base64.encodeBase64String(("username:password").getBytes()));
        connection.setConnectTimeout(60000);
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            // OK
            ByteArrayOutputStream bais = new ByteArrayOutputStream();
            InputStream is = null;
            try {
              is = connection.getInputStream();
              byte[] byteChunk = new byte[4096];
              int n;

              while ( (n = is.read(byteChunk)) > 0 ) {
                bais.write(byteChunk, 0, n);
              }
            }
            catch (IOException e) {

            }
            finally {
              if (is != null) { is.close(); }
            }
            httpResponse.setContentType("application/pdf");
            httpResponse.setHeader("content-disposition","attachment; filename=yyyyy.pdf");             
            httpResponse.getOutputStream().write(bais.toString().getBytes("UTF-8"));
            httpResponse.getOutputStream().flush();         
        }
....
}

With Firebug i see the repsonse: 使用Firebug,我看到了回复:

%PDF-1.3
%âãÏÓ
2 0 obj
<<
/Type /FontDescriptor
/Ascent 720
/CapHeight 660
/Descent -270
/Flags 32
/FontBBox [-177 -269 1123 866]
/FontName /Helvetica-Bold
/ItalicAngle 0
....

What i need to set in ajax's function to show the pdf? 我需要在ajax函数中设置什么才能显示pdf?

Thanks in advance 提前致谢

I don't know Java well, but in my understanding your mechanism may not be right. 我不太了解Java,但是据我了解,您的机制可能不正确。

Here are my corrections: 这是我的更正:

Instead of sending files in stream, the server-side code(JAVA) should generate the pdf at backend, put the file in file system, and then return the URI of file to Ajax response. 服务器端代码(JAVA)不应在流中发送文件,而应在后端生成pdf,将文件放入文件系统中,然后将文件的URI返回给Ajax响应。

For Ajax code, it get the url from server, then show the new url in DOM. 对于Ajax代码,它从服务器获取URL,然后在DOM中显示新的URL。 Then user can follow this link to read/download PDF. 然后,用户可以点击此链接阅读/下载PDF。

Side note: 边注:

I checked further that there are methods for streaming data by Ajax, though jQuery's ajax() can't handle that. 我进一步检查了Ajax有一些流数据的方法,尽管jQuery的ajax()无法处理。 But I think for a PDF file rendering, streaming is overkill. 但我认为,对于PDF文件渲染而言,流式传输是过大的。 Refs: jquery ajax, read the stream incrementally? 引用: jquery ajax,以增量方式读取流? , http://ajaxpatterns.org/HTTP_Streaming#In_A_Blink * http://ajaxpatterns.org/HTTP_Streaming#In_A_Blink *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM