Reputation: 738
I want read the content of file and store it in array. I have a code to browse a file using formPanel and formupload.
How to read a file and store in array.
Any sample code will be of great Help.
Upvotes: 3
Views: 6963
Reputation: 3717
You can use the HTML5 file reader. Something like this:
@UiField FileUpload fileUploadWidget;
JavaScriptObject files = fileUploadWidget.getElement().getPropertyJSO("files");
readTextFile(files);
public static void fileLoaded(String fileContents) {
GWT.log("File contents: " + fileContents);
}
public static native void readTextFile(JavaScriptObject files)
/*-{
var reader = new FileReader();
reader.onload = function(e) {
@com.example.YourClass::fileLoaded(*)(reader.result);
}
return reader.readAsText(files[0]);
}-*/;
Upvotes: 5
Reputation: 131
Try use elemental library from GWT:
http://www.gwtproject.org/articles/elemental.html
Example code reading file content to txtArea (all client site):
import elemental.client.*;
import elemental.dom.*;
import elemental.html.*;
FileEntry fileEntry = (FileEntry)entry;
FileCallback callback = new FileCallback() {
public boolean onFileCallback(File file) {
final FileReader reader = window.newFileReader();
reader.setOnloadend(new EventListener() {
public void handleEvent(Event evt) {
txtArea.setText(reader.getResult().toString());
}
});
reader.readAsText(file);
return true;
}
};
fileEntry.file(callback, errorCallback);
This tutorial may be also useful: http://www.instantshift.com/2013/11/19/html5-features-with-gwt-elemental/
Upvotes: 2
Reputation:
GWT client code is simply JavaScript and is constrained by the same limitations that any regular JavaScript would be subject to in the browsers sandbox model.
That is to say no local file access is available to JavaScript from any browser.
The file dialog box and the resulting transfer to the server is handled by the browser and is not available to the JavaScript code in any fashion.
You would have to upload the file to a server and then process the file on the server and display what you needed to display by sending the data back to the client from the server.
Upvotes: 2
Reputation: 4363
You could use Flash or a Java Applet to do it all client-side. But those are complicated and probably not very beautiful solutions.
Have a look at GwtAI for Java Applet integration in GWT: http://code.google.com/p/gwtai/
Upvotes: 0
Reputation: 47608
1) Upload the file to your server (using gwt-upload for example)
2) Then make a request from your client to the server to retrieve the bytes of the file
Upvotes: 0