使用 Servlet 和 Javascript 导出 csv
我正在开发一个网络应用程序,在某个时刻,用户向服务器(servlet)请求一些内容,服务器应该向用户返回一个 csv 文件。此时我需要向用户显示一个对话框,让您可以选择指定要保存 csv 文件的目录。
在 Servlet 中,我有:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/csv");
response.setHeader("content-disposition","filename=test.csv"); // set the file name to whatever required..
PrintWriter out = response.getWriter();
out.println("a,b,c,d,e,f");
out.println("1,2,3,4,5,6");
out.flush();
out.close();
}
在 javascript 中,我有:
$.get('URL_SERVLET' , function(data) {
alert("ret: " + data);
});
下一步如何?
编辑:
我使用以下简单命令得到了很好的结果:
<a href='http://localhost:8080/MY_SERVLET'> link here </ a>
单击链接,会自动下载文件 test.csv
为了满足我的需求,我需要“href”内容是动态的,并且通过单击按钮来触发。
示例:
用户选择名称为“radio 1”的无线电组件,然后单击“导出 csv”按钮,这次 url 将动态构建,例如:“http://localhost:8080/MY_SERVLET/csv1”,文件将为自动下载(与上面包含文本“链接此处”的链接相同)
可能吗?
谢谢。
I am developing a web app that in some moment the user requests something for the server (servlet) and the server should return a csv file to the user. I need at this point a dialog box appears to the user, giving you the option to specify the directory where you want to save the csv file.
In Servlet I have:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/csv");
response.setHeader("content-disposition","filename=test.csv"); // set the file name to whatever required..
PrintWriter out = response.getWriter();
out.println("a,b,c,d,e,f");
out.println("1,2,3,4,5,6");
out.flush();
out.close();
}
In javascript I have:
$.get('URL_SERVLET' , function(data) {
alert("ret: " + data);
});
How is the next step?
EDIT:
I got a good result using the simple following command:
<a href='http://localhost:8080/MY_SERVLET'> link here </ a>
Clicking on the link, the file test.csv is automatically downloaded
To Answer my need, I need that "href" content was dynamic and was fired by the click of a button.
example:
User select radio component with name "radio 1" and clicked the button "Export csv", this time the url would be built dynamically, example: "http://localhost:8080/MY_SERVLET/csv1" and the file would be downloaded automatically (equal what happens on the link above that contains the text "link here")
Is possible?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可能指定下载文件的“目录”,因为:
可能的解决方案:
There is no possible specify the "directory" for download a file, because:
Possible solutions:
您无法使用 ajax 触发另存为对话框。您只需将浏览器导航至下载网址即可。它将检测正确的标题并显示另存为对话框。
You can't trigger the save-as dialog with ajax. You should simply navigate the browser to the download url. It will detect the proper headers and will show the save-as dialog.
您无法使用 JavaScript 强制另存为对话,因此您最终会得到无法使用的响应。您需要使用
window.location
而不是 ajax 请求。You cannot force a Save As dialogue with JavaScript, so you end up with an unuseable response. You need to use
window.location
instead of an ajax request.