tags:

views:

196

answers:

2

Hi all! I need to read a file located on a server but I see that in GWT is not possible use some java library. what I have to do?

A: 

try requestBuilder!! this code can help?

        RequestBuilder requestBuilder = new RequestBuilder( RequestBuilder.GET, "yourfile.txt" );
        try {
            requestBuilder.sendRequest( null, new RequestCallback(){
                public void onError(Request request, Throwable exception) {
                    GWT.log( "failed file reading", exception );
                }

                public void onResponseReceived(Request request, Response response) {
                    String result=response.getText();

                }} );
        } catch (RequestException e) {
            GWT.log( "failed file reading", e );
        }
Kerem
did it, but seem that result does not contain any character
alessandro
http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/index.htmlcheck here.Maybe HttpRequest or something like that can solve your problem.what kind of file do you want to fecth?
Kerem
it's a .txt file
alessandro
place your txt file in /war/<prjctName>
Kerem
thank you for the help.I just need to read some bytes of a file having his url(for example: h_ttp://w_ww.abc.it/FILE.txt)
alessandro
no problem :)but you can not make an ajax request to other domain. This is a browser limit for security(check https://developer.mozilla.org/En/HTTP_access_control). I think, you need to check cross domain scripting (http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html).Or your server side can fetch the data and you can get it from your server.
Kerem
A: 

The Rule: JavaScript cannot read data from a URL that doesn’t have a host name and port that matches those of the page the JavaScript is running in.

In other words: If it is on a different site — you can’t read it directly with JS and therefore GWT, which is nothing more than Javascript once compiled.

It applies to data from XMLHttpRequest, frames, and anything else you care to name.

This may change in the future, but for now the rule stands.

With this in mind there are a couple of workarounds.

1) Call your server with RPC or whatever mechanism and have your server do the request and then send it back to the client. Here is a sample.

2) There are several hacks on allowing JavaScript to access cross-domain sites just do a google search on how to get this. Some browsers will flag this as being dangerous.

3) If you are using Firefox and Firefox only it looks like Firefox has the ability to do this, but you will need to enable this manually.

Romain Hippeau