views:

525

answers:

2

Does anybody know how to read google gears blob objects within the browser? I'm using gwt on top of gears, but I'm looking for any kind of solutions. The application needs to work fully offline so I can't post the files and process them server side. My files are simple text files that I want to upload and parse in offline mode.

A: 

Have you looked at the Google Gears API documentation (for JavaScript)?

Ionuț G. Stan
Yes I have. it only provides following methods: readonly attribute int length, Blob slice(offset, length).I have tried with the best of my Javascript skills to inspect the blob object but all I can get out of it is its lenght, everything else is just encoded characters. I assume that the purpose is that I'm not supposed to read it. The question remains if there is any other trick I can use to read it. I read some comments somewhere about putting the resource in the localserver/database and somehow read it from there but I'm quite lost how to do it and if it will actually work
Salla
+1  A: 

I wrote a very simple class to do this you can check it out here: http://procbits.com/2009/07/29/read-file-contents-blobs-in-gwt-and-gears/

It's very simple to use. Either call the method "readAllText" or you can read it line by line. Here is an example reading line by line:

try {
 Desktop dt = Factory.getInstance().createDesktop();
 dt.openFiles(new OpenFilesHandler(){
  public void onOpenFiles(OpenFilesEvent event) {
   File[] files = event.getFiles();
   File file = files[0];
   Blob data = file.getBlob();

   BlobReader br = new BlobReader(data);
   while (!br.endOfBlob())
    Window.alert(br.readLine());
  }
 }, true);
} catch (Exception ex){
 Window.alert(ex.toString());
}

I hope this helps!

JP
You are a champion! Thanks so much, this made a big difference!
Salla