使用 Servlet 和 Javascript 导出 csv

发布于 2025-01-02 02:58:43 字数 1203 浏览 0 评论 0原文

我正在开发一个网络应用程序,在某个时刻,用户向服务器(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山有枢 2025-01-09 02:58:43

不可能指定下载文件的“目录”,因为:

  1. HTTP 协议仅允许您指定文件的名称,然后您无法从 Servlet 建议目录。尊重下载文件的位置取决于浏览器配置,这不是标准
  2. JavaScript 不允许您写入磁盘,那么您将无法接收 CSV 并稍后将其写入磁盘。

可能的解决方案:

  1. 您可以使用与 servlet 联系的 Applet,然后将文件写入所需的目录。如果您决定这样做,那么您将需要对该小程序进行签名。
  2. 你可以使用Flash、ActiveX等(我不知道这些方法的细节)

There is no possible specify the "directory" for download a file, because:

  1. The protocol HTTP just enable you to specify the name of the file then you can't suggest the directory from Servlet. Respect where download the file it will depend of browser configuration and this isn't standard
  2. JavaScript don't allow to you write to disk then you can't receive the CSV and later write it to disk.

Possible solutions:

  1. You can use an Applet that contact with the servlet and then write the file to desired directory. If you decide do this then you will need sign the applet.
  2. You can use Flash, ActiveX, etc. (I don't know the details for these aproaches)
生寂 2025-01-09 02:58:43

您无法使用 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.

ぃ双果 2025-01-09 02:58:43

您无法使用 JavaScript 强制另存为对话,因此您最终会得到无法使用的响应。您需要使用 window.location 而不是 ajax 请求。

window.location = 'URL_SERVLET';

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.

window.location = 'URL_SERVLET';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文