[英]ajax response as pdf, how to show it?
我有一个生成pdf的网络服务。 在我的GAE应用程序中,我有一个按钮,当我单击时,我使用ajax的功能。
$('#test').click(function(){
$.ajax({
url: 'provaws.do',
type: 'get',
dataType: 'html',
success : function(data) {
}
});
});
这是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();
}
....
}
使用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
....
我需要在ajax函数中设置什么才能显示pdf?
提前致谢
我不太了解Java,但是据我了解,您的机制可能不正确。
这是我的更正:
服务器端代码(JAVA)不应在流中发送文件,而应在后端生成pdf,将文件放入文件系统中,然后将文件的URI返回给Ajax响应。
对于Ajax代码,它从服务器获取URL,然后在DOM中显示新的URL。 然后,用户可以点击此链接阅读/下载PDF。
边注:
我进一步检查了Ajax有一些流数据的方法,尽管jQuery的ajax()
无法处理。 但我认为,对于PDF文件渲染而言,流式传输是过大的。 引用: jquery ajax,以增量方式读取流? , http://ajaxpatterns.org/HTTP_Streaming#In_A_Blink *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.