Java – using requestbuilder to process attachments in GWT

I am sending an HTTP post request from GWT client to HTTP servlet The servlet is creating a PDF file from the request content and writing it to the response stream

The title of the response flow is:

Content-Disposition: attachment; filename=report.pdf

I want to open this PDF in a new window in 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 the onresponsereceived response be handled?

Solution

I don't think you should use a single requestbuilder Ajax call in this case You can rely on the default browser behavior by calling the normal call and let the browser process the PDF response (display or open the save dialog box with the PDF viewer plug-in)

There are several options to achieve this:

>If you can pass data through a get request (only for small data volumes), you can use the data as a get parameter to create a URL, and then open a new browser window, where window Open() passes the URL to the data. > For a large amount of data, you can temporarily store the data of requestbuilder and the post data of the server in requestcallback Onresponsereceived() opens a new browser window with a short URL as described above On the server side, you must split the PDF generation servlet into two parts: the data storage servlet with post method (that is, the data is stored in the web session) and the PDF rendering servlet with get method, which deletes the data from the session (and deletes) without large parameters. > Create a form, method post, hidden fields of data and PDF to generate servlet URL Fill in the hidden fields with data and submit the form programmatically (i.e. formpanel. Submit()) If you create a formpanel with target name, the browser will open a new window or use the specified frame to process the response

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>