通过 gwt (http get) 开始从 servlet 下载

发布于 2024-12-01 01:38:14 字数 1675 浏览 1 评论 0原文

我想从我的 gwt Web 应用程序中的 clickevent 开始文件下载。所以我编写了一个 Servlet,它将数据写入输出并开始下载。通过http get 接收数据。

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String data = request.getParameter("data");
    String filename = request.getParameter("filename");
    byte[] streamData = data.getBytes();
    response.setContentType("application/force-download");
    response.setHeader("Content-Disposition", "attachment; fileName="
            + filename + ".csv");
    response.setContentLength(streamData.length);

    ServletOutputStream out = response.getOutputStream();
    out.write(streamData);
    out.flush();
}

在客户端中,我通过 requestBuilder.sendRequest() 启动 get 方法:

RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, /download");
requestBuilder.sendRequest("filename=dues&data="+ theDataAsString, new RequestCallback() {

                    @Override
                    public void onResponseReceived(Request request,
                            Response response) {
                        // Anything to do here?
                    }

                    @Override
                    public void onError(Request request, Throwable exception) {
                        exception.printStackTrace();
                        SC.warn("Error while creating export file.");
                    }
                });

没有任何反应。但为什么?浏览器不应该要求开始下载吗?

在这种情况下,我使用 post 还是 get 有关系吗?

我不想使用类似

Window.open("/download?data=myData&filename=filename", "_blank", "");

“有什么想法吗?”之类的东西。

I would like to start a file download from a clickevent in my gwt web app. So I wrote a Servlet which writes the data to the output and should start the download. The data is received via http get.

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String data = request.getParameter("data");
    String filename = request.getParameter("filename");
    byte[] streamData = data.getBytes();
    response.setContentType("application/force-download");
    response.setHeader("Content-Disposition", "attachment; fileName="
            + filename + ".csv");
    response.setContentLength(streamData.length);

    ServletOutputStream out = response.getOutputStream();
    out.write(streamData);
    out.flush();
}

In the client I start the get method via requestBuilder.sendRequest():

RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, /download");
requestBuilder.sendRequest("filename=dues&data="+ theDataAsString, new RequestCallback() {

                    @Override
                    public void onResponseReceived(Request request,
                            Response response) {
                        // Anything to do here?
                    }

                    @Override
                    public void onError(Request request, Throwable exception) {
                        exception.printStackTrace();
                        SC.warn("Error while creating export file.");
                    }
                });

Nothing happens. But why? Shouldn't the browser ask to begin a download?

Does it, in this case, matter if i use post or get?

I don't want to use somthing like

Window.open("/download?data=myData&filename=filename", "_blank", "");

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

仙气飘飘 2024-12-08 01:38:14

在客户端,使用 Anchor 而不是请求构建器并直接调用 servlet。

On the client side, use an Anchor instead of a request builder and invoke the servlet directly.

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