在GWT中处理RequestBuilder的附件 [英] Handle attachment in response with RequestBuilder in GWT

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

问题描述

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

响应流的标题是:

 内容处理:附件; filename = report.pdf 

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

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

字符串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 '连接到服务器(可能是超时,违反SOP等)
}

public void onResponseReceived(Request request,Response response){
if(200 == response .getStatusCode()){
//在response.getText()中处理响应
// Window.open(url,_blank,);
} else {
//处理错误,可以从response.getStatusText()
}
}
}}获取状态文本;
} catch(RequestException e){
//无法连接到服务器
}

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

解决方案

我认为在这种情况下,您不应该使用单个 RequestBuilder AJAX调用。您可以通过调用普通调用并让浏览器处理PDF响应(显示PDF阅读器插件或打开保存对话框)来依靠默认浏览器行为。



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


  1. 如果您可以在GET请求中传递数据(仅适用于小型数据量),您可以将数据创建为GET参数,然后使用 Window.open()通过URL传递数据。对于大量的数据,您可以首先使用 RequestBuilder 将数据发布到服务器以存储数据并在 RequestCallback.onResponseReceived()打开一个新的浏览器窗口1.在服务器端,必须将PDF生成servlet分为两部分:带POST方法的数据存储servlet(即将数据存储到Web会话中)和使用GET方法的PDF呈现servlet,该方法将数据从会话(并删除它)并且不需要大的参数。

  2. 使用POST方法创建一个表单,为您的数据和PDF生成servlet隐藏字段URL。用数据填充隐藏字段并以编程方式提交表单(即 FormPanel.submit())。如果您使用 FormPanel /user/client/ui/FormPanel.html#FormPanel%28java.lang.String%29rel =noreferrer>目标名称,浏览器打开一个新窗口或使用指定的框架来处理响应。 / p>



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.

Headers of the response stream are:

Content-Disposition: attachment; filename=report.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        
}

How should I handle response in onResponseRecieved?

解决方案

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. 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.

  2. 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.

  3. 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天全站免登陆