Monday, June 4, 2012

Re: XMLHttpRequest getResponse instead of getResponseText to get arraybuffer data

Hi, I tried to use your solution. Looking at Google I/O on youtube (http://www.youtube.com/watch?v=NNmoEOpGJdk) I saw this but I wasn't able to do it.

I tried creating this:

public class BinaryXMLHttpRequest extends XMLHttpRequest
{
    public native JavaScriptObject getResponse()
    /*-
    {
        return this.response;
    }
    -*/;
   
    //Set to "arraybuffer" or "blob" for binary response
    public native void setResponseType(String value)
    /*-
    {
       this.responseType = value;
    }
    -*/;
}

then I used this class this way:

BinaryXMLHttpRequest binxhr = (BinaryXMLHttpRequest)BinaryXMLHttpRequest.create();
binxhr.open("GET", "myservlet_url");
binxhr.setResponseType("arraybuffer");
       
binxhr.setOnReadyStateChange(new ReadyStateChangeHandler()
{
    @Override
     public void onReadyStateChange(XMLHttpRequest xhr)
     {
         BinaryXMLHttpRequest binxhr = (BinaryXMLHttpRequest)xhr;
         if( binxhr.getReadyState() == XMLHttpRequest.DONE )
         {
             binxhr.clearOnReadyStateChange();
             binxhr.getResponse(); => what can I do with this?
         }
    }
});

If you look, at the end I have a binxhr.getResponse(); But this function returna a JavaScriptObject type. What can I do now with this? This is not a byte[] or whatever, so, I don't know in GWT how to manipulate it. Do you have an idea?


Il giorno lunedì 4 giugno 2012 13:45:34 UTC+2, Thomas Broyer ha scritto:

On Monday, June 4, 2012 1:03:40 PM UTC+2, Magallo wrote:
Hi all,
I qoute from https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data

in javascript is possible, using the The XMLHttpRequest Level 2 Specification adds new responseType attributes which make sending and receiving binary data much easier.

Something like this:
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);  

xhr.requestType = "arraybuffer";   
xhr.onload = function(e)
{  

  var arraybuffer = xhr.response; // not responseText  
  /* ... */  
}  
xhr.send(); 

How to get this in GWT?

 
The class XMLHttpRequest has not a setResponseType(...) method and also has not a getResponse() method to get data, for example, as a byte[] or something similar, but has only a getResponseText() method. Is there a way to solve this?

XMLHttpRequest is a JavaScriptObject, so it's easy to add those: either subclass XMLHttpRequest and then cast() any XMLHttpRequest to your subclass, or use static native helper methods.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/8-nDeDPl_0cJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment