3

i've got the follow issue: I wanna implement a file-reader in my GWT-application which allows the user to upload a file and work with it's content (in this particular case I'm talking about HTML). How do I realize that? As far as I know there is no way to use Java's usual BufferedReader. The only thing I've discovered so far is the following:

        try {
        Request r = new RequestBuilder(RequestBuilder.GET, file).sendRequest("", new RequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                String text = response.getText();
                System.out.println("1234");
            }

            @Override
            public void onError(Request request, Throwable exception) {
                System.out.println("456");
            }

            });
    } catch (RequestException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But this wouldn't be that helpful as far as there's no file-chooser dialog.

I'd realy appreciate your help!

Regards

otter-in-a-suit
  • 1,067
  • 5
  • 19
  • 32

5 Answers5

3

There is no ready to use FileReader API in GWT. You can build your own on top of JS Api http://www.html5rocks.com/en/tutorials/file/dndfiles/ for Firefox/Chrome and you'll have to use some kind of plugin for IE (flash for example) in order to make it work everywhere

jusio
  • 9,585
  • 1
  • 39
  • 55
1

Here is another way to read a file on the client side using the File Reader using the GWT Elemental API.

https://gist.github.com/branflake2267/180b69b9a29987214643f62fb279151f

Brandon
  • 1,783
  • 17
  • 24
1

You could upload the file to the server using the FileUpload, then immediately download the file from the server to the client, using RequestBuilder or GWT RPC, where you can then work with it. You wouldn't be able to use InputStreams or Readers as these are not actually emulated by GWT (not strictly true, see below).

If it's HTML you need to be careful - there are all sorts of security implications in allowing user generated HTML onto a page. The GWT SafeHTML mechanism can be used to address some of these issues but it is still up to you to manage this.

As an aside it is trivial to create an implementation of streams/readers that does work in GWT - the java.io.InputStream pretty much works in GWT as is but I am not sure if you can just copy this class for licensing reasons. The Apache Harmony project includes implementations that you may be able to "copy" without these restrictions.

pillingworth
  • 3,128
  • 2
  • 21
  • 46
0

You can't implement client-side file reader, just because of limitations of JavaScript. Client-side GWT code is compiled to JavaScript code and then JavaScript is executed within the browser. It has no access to local files or whatever.

Check this for file uploading: http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload

Andrey Agibalov
  • 7,487
  • 7
  • 64
  • 110
  • 1
    This related question indicates that JS _can_ read local files. http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded – Brian White May 12 '14 at 00:45
-1

Did you have a look at this: https://developers.google.com/appengine/kb/java?hl=en#readfile ?

girl
  • 69
  • 7