Question

I'm developing Zimbra Zimlet. I'm requesting JSP from Javascript (both belong to the same Zimlet)

var jspUrl = this.getResource("my.jsp");
var callback = new AjxCallback(this, this._rpcCallback, ["param1", "param2"]);
AjxRpc.invoke(null, jspUrl, null, callback, true);

_rpcCallback function

automator_HandlerObject.prototype._rpcCallback = function(p1, p2, response) {
    if (response.success == true) {
        appCtxt.getAppController().setStatusMsg(response.text);
    } else {
        console.log("response error");
    }
}

I need to return some binary file in response to that request. Here is JSP code

<%@ 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" also were tested with FireFox and Chrome)

I expected "save file" browser dialog to be appeared.

I tried

document.write(response.text)

in _rpcCallback function and I can see appropriate response headers

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

as well as binary response body content, but nothing happened.

What code _rpcCallback function must contain in order to show "download file" dialog instead of printing the file as text?

Tested with Zimbra Desktop 7.2.2 GA

Était-ce utile?

La solution

Thanks to Julian the solution has been found and it is too simple:

window.open(fileUrl);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top