如何强制浏览器打开“另存为”二进制响应对话框
How to force browser to open "save as" dialog for binary response?
我正在开发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);
_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函数必须包含为了显示"下载文件"对话框,而不是打印文件作为文本?
使用Zimbra Desktop 7.2.2 GA测试
感谢Julian找到了解决方案,它太简单了:
window.open(fileUrl);
相关文章:
- 无法在本地计算机中将画布另存为图像
- 如何将 html5 画布另存为窗口 8 Metro 应用程序中的图像文件
- 试图制作一个“;另存为“;使用php's”;file_put_contents”;和javascript&quo
- Safari浏览器上的“强制另存为”对话框
- 强制浏览器打开“;另存为“;对话框
- execCommand('另存为',true,'data.csv');不在IE工作
- 更改音频速度,然后另存为新文件
- 将响应另存为文件
- 覆盖 HTML 画布上的“另存为”行为
- 如何在浏览器中触发另存为对话框,以便可以在内存中保存 json 数据
- 更改“将图像另存为”的操作以重定向到相应的网页
- 如何为图像创建“另存为”按钮
- 将 JavaScript 对象另存为字符串
- 如何定义图像'另存为'大小
- 使用javascript将画布另存为图像
- 嵌套画布另存为单个图像
- 如何在Chrome中禁用“另存为”对话框
- 将画布对象另存为图像-黑色背景
- 使用JavaScript,我可以上传一个word文件并使用.replaces然后另存为新文档吗
- 如何强制浏览器打开“另存为”二进制响应对话框