How do I handle file uploads with GWT?
I was looking through for some solutions to read a file after uploading and I found this:
Read text file in google GWT?
that has a solution of
new RequestBuilder(Method.GET, "path/to/file.txt").sendRequest("", new RequestCallback() {
@Override
public void onResponseReceived(Request req, Response resp) {
String text = resp.getText();
// do stuff with the text
}
@Override
public void onError(Request res, Throwable throwable) {
// handle errors
}
});
It seems to be a feasible solution for my case, but I am kinda new to this, can any one explain how can I apply this in gwt? i have FileUpload 开发者_Python百科placed in a panel already and a click handler to handle the submit button click.
Can someone help me out with this?
The answer you link to is for reading files from the server. They are just requesting the file from the webserver. It sounds like you want to read files from the client (you are using a FileUpload). There are different methods of doing that based on the stack your app is running on and what clients you support.
The GWT FileUpload is just an input control on the form which allows the user to pick a file. It does not do any part of the actual file reading.
A common approach is to send the file as part of the HTML form to the server and then reflect it back to the client to get it into your web app. This site is a little old but is great at giving you the basics of this approach. There are several examples using this with GWT: for example which also links to this. This option has the widest client support, but costs more in network traffic.
If you know the clients support HTML5 you can also read the file in JavaScript using the File API. Here are some docs from Mozilla. Unfortunately, there's not really integrated GWT support for it, so you'll have to write some JavaScript. This option will not be supported by all clients, but also doesn't generate any network traffic.
精彩评论