Quantcast
Channel: Zimbra :: Forums - Zimlets
Viewing all articles
Browse latest Browse all 171

How to return a file for download by Zimbra Zimlet?

$
0
0
I'm requesting JSP from Javascript (both belong to the same Zimlet)

Code:

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

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

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/x-download");
    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();
%>

I also tried "application/force-download" and "application/octet-stream" as "contentType".

I expect a browser to show "download file" dialog but instead of that file content is printed as text

file_content_toast.jpg

How to fix that?
Attached Images

Viewing all articles
Browse latest Browse all 171

Trending Articles