我正在开发 Zimbra 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);
_rpc回调函数
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函数必须包含哪些代码才能显示“下载文件”对话框而不是将文件打印为文本?
使用 Zimbra Desktop 7.2.2 GA 测试