我在开发Zimlet。我从Javascript请求JSP (都属于同一个Zimlet)
var jspUrl = this.getResource("my.jsp");
var callback = new AjxCallback(this, this._rpcCallback, ["param1", "param2"]);
AjxRpc.invoke(null, jspUrl, null, callback, true);_rpcCallback函数
automator_HandlerObject.prototype._rpcCallback = function(p1, p2, response) {
if (response.success == true) {
appCtxt.getAppController().setStatusMsg(response.text);
} else {
console.log("response error");
}
}我需要返回一些二进制文件来响应这个请求。这是JSP代码
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.BufferedInputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.IOException" %>
<%
ServletOutputStream outStream=response.getOutputStream();
File myfile = new File("/tmp/attachment.zip");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=attachment.zip");
response.setContentLength( (int) myfile.length( ) );
FileInputStream input = new FileInputStream(myfile);
BufferedInputStream buf = new BufferedInputStream(input);
int readBytes = 0;
while((readBytes = buf.read( )) != -1)
outStream.write(readBytes);
outStream.flush();
outStream.close();
buf.close();
%>("application/x-download"/"application/force-download“也用FireFox和Chrome进行了测试
我希望“保存文件”浏览器对话框会出现。
我试过了
document.write(response.text)在_rpcCallback函数中,我可以看到适当的响应头。
HTTP/1.1 200 OK
Date: Fri, 03 May 2013 08:16:49 GMT
Expires: Thu, 01-Jan-1970 00:00:00 GMT
Content-Type: application/octet-stream
Content-Length: 20021
Set-Cookie: JSESSIONID=11ebfk145b34z;Path=/zimlet
Content-Disposition: attachment;filename=attachment.zip以及二进制响应体的内容,但是什么都没有发生。
为了显示“下载文件”对话框而不是将文件打印为文本,_rpcCallback函数必须包含哪些代码?
使用7.2.2GA进行测试
发布于 2013-05-04 16:09:15
多亏朱利安找到了解决办法,这太简单了:
window.open(fileUrl);https://stackoverflow.com/questions/16332879
复制相似问题