使用 GWT 中的 RequestBuilder 响应附件处理附件 [英] Handle attachment in response with RequestBuilder in GWT

查看:40
本文介绍了使用 GWT 中的 RequestBuilder 响应附件处理附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 GWT 客户端向 HTTPServlet 发出 HTTP POST 请求.此 Servlet 正在从请求内容创建 PDF 文件并将其写入响应流.

I am making a HTTP POST request from GWT Client to a HTTPServlet. This Servlet is creating a PDF file from request content and writing it to response stream.

响应流的头部是:

Content-Disposition: attachment; filename=report.pdf

我想在用户浏览器的新窗口中打开此 PDF 或提示他下载.

I want to open this PDF in new window of the user's browser or prompt him to download it.

import com.google.gwt.http.client.*;
...

String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));

try {
  Request request = builder.sendRequest(data, new RequestCallback() {
    public void onError(Request request, Throwable exception) {
       // Couldn't connect to server (could be timeout, SOP violation, etc.)     
    }

    public void onResponseReceived(Request request, Response response) {
      if (200 == response.getStatusCode()) {
          // Process the response in response.getText()
          // Window.open(url, "_blank", "");
      } else {
        // Handle the error.  Can get the status text from response.getStatusText()
      }
    }       
  });
} catch (RequestException e) {
  // Couldn't connect to server        
}

我应该如何处理 onResponseRecieved 中的响应?

How should I handle response in onResponseRecieved?

推荐答案

我认为在这种情况下您不应该使用单个 RequestBuilder AJAX 调用.您可以通过调用正常调用并让浏览器处理 PDF 响应(使用 PDF 查看器插件显示它或打开保存"对话框)来依赖默认浏览器行为.

I think in this case you should not use a single RequestBuilder AJAX call. You can rely on the default browser behavior by invoking a normal call and letting the browser handle the PDF response (displaying it with a PDF viewer plugin or opening a Save dialog).

有几种方法可以实现这一点:

There are several alternatives to achieving this:

  1. 如果您可以在 GET 请求中传递数据(仅适用于小数据量),您可以使用数据作为 GET 参数创建 URL,然后使用 Window.open() 传递带有数据的 URL.

  1. If you can pass your data in a GET request (only possible for a small data volume) you can create the URL with the data as GET parameters and then open a new browser window with Window.open() passing the URL with data.

对于更大量的数据,您可以首先使用 RequestBuilder 将数据发布到服务器,以便将数据临时存储在 RequestCallback.onResponseReceived() 打开一个新的浏览器窗口,其中包含一个类似于上面替代方案 1 的短 URL.在服务器端,您必须将 PDF 生成 servlet 分成两部分:一个带有 POST 方法的数据存储 servlet(即将数据存储到 Web 会话中)和一个带有 GET 方法的 PDF 呈现 servlet,它从会话中取出数据(并删除它)并且不需要大参数.

For larger amounts of data you can first POST your data with RequestBuilder to the server to store the data temporaly and in RequestCallback.onResponseReceived() open a new browser window with a short URL like above in alternative 1. On the server side you have to split the PDF generation servlet into two parts: a data store servlet with POST method (i.e. storing the data into the web session) and a PDF render servlet with GET method, which takes the data out of the session (and deleting it) and doesn't need large parameters.

使用 POST 方法、数据的隐藏字段和 PDF 生成 servlet URL 创建表单.用您的数据填充隐藏字段并以编程方式提交表单(即 FormPanel.submit()).如果您使用 目标名称 浏览器打开一个新窗口或使用指定的框架来处理响应.

Create a form with method POST, hidden fields for your data and the PDF generation servlet URL. Fill the hidden fields with your data and submit the form programmatically (i.e. FormPanel.submit()). If you create your FormPanel with a target name the browser opens a new window or uses the specified frame to handle the response.

这篇关于使用 GWT 中的 RequestBuilder 响应附件处理附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆