Upload File from GWT to another domain , response is always null

Viewed 115

I am uploading a File from GWT to a different domain

File Uploads well , But the response i sent from the server always reaches as "null" at the client side

                    response.setContentType("text/html");
                    response.setHeader("Access-Control-Allow-Origin", "*");
                    response.getWriter().print("TEST");

response is NULL only when i upload the file on a different domain ... (on same domain all is OK)

I also see this in GWT documentation

 Tip:
 The result html can be null as a result of submitting a form to a different domain.

http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/FormPanel.SubmitCompleteEvent.html

Is there any way I can receive back a response at my client side when i am uploading file to a different domain

1 Answers

There are 2 possible answer:

  1. Use JSONP Builder
    JsonpRequestBuilder requestBuilder = new JsonpRequestBuilder();
    requestBuilder.requestObject(url, new AsyncCallback<FbUser>() {
    @Override
    public void onFailure(Throwable ex) {
        throw SOMETHING_EXCEPTION(ex);
    }

    @Override
    public void onSuccess(ResponseModel resp) {
        if (resp.isError()) {
            // on response error on something
            log.error(resp.getError().getMessage())
            log.error(resp.getError().getCode())
        }
        
        log.info(resp.getAnyData())
    }
  1. Not to use GWT to upload, rather use other client like apache HttpClient
public uploadFile() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    FileBody bin = new FileBody(new File(UPLOADED_FILE));
    long size = bin.getContentLength();

    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("PART", bin);
    String content = "-";
    try {
         httpPost.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            HttpEntity ent = response.getEntity();
            InputStream st = ent.getContent();
            StringWriter writer = new StringWriter();
            IOUtils.copy(st, writer);
            content = writer.toString();                

    } catch (IOException e) {                   
        return "false";
    }
    return content;
}

Hope it helps

Related