How to get a file from the GWT FileUpload component?

I want to upload a file using the GWT component fileUploader,

I tried like this:

FileUpload fileUpload = new FileUpload(); filepload.addChangeHandler(new new ChangeHandler() { @Override public void onChange(ChangeEvent event) { // here i submit the form, and a response is created to client side to display success message. } }); 
  • Ok, so far I can upload the file, and from the servlet I can give a message about the successful completion.
  • I processed it using the onSubmitCompleate event of the form panel,
  • thanks allot.
  • Let me ask one more thing, is there anyway I can display the downloaded file before saving it to the database?
  • ie, in fact, I need to provide a composite component for loading the file and the text area in order to provide details about the file.
  • and when the user selects the file to upload, I want to provide a display of the downloaded file (so that it can ensure that the correct file is downloaded).
  • and it will be saved in db when the whole form is submitted.
+3
file-upload gwt
source share
3 answers

I assume that you want to allow the user to download the file using the GWT Fileupload widget, and then you do not want to process it on the server side. You need a byte array representation on the client side.

The usual steps for processing files are Browser → File upload dialog → Select file → Send form with file to server → Process file on server → Send processed file to client as response (line).

If you want to avoid the steps described above and process the file in a browser, there is no way to do this in the current javascript. Parallel technologies such as Flash, Applet, Silverlight, or Activex can help. The correct approach in the future will be to use an HTML5 apis file .

If you do not want to use outdated technologies such as flash / applet, then you can learn HTML5 apis on FileReader. However, the tradeoff is that you need to check if api is supported in the browser.

HTML5 FileReader

FileReader includes four options for reading a file, asynchronously:

 FileReader.readAsBinaryString(Blob|File) - The result property will contain the file/blob data as a binary string. FileReader.readAsText(Blob|File, opt_encoding) - The result property will contain the file/blob data as a text string. FileReader.readAsDataURL(Blob|File) - The result property will contain the file/blob data encoded as a data URL. FileReader.readAsArrayBuffer(Blob|File) - The result property will contain the file/blob data as an ArrayBuffer object. 

An example of a GWT wrapper over them - https://github.com/bradrydzewski/gwt-filesystem

Link -

+3
source share

You can get the file name and its extension on the client side (in your gwt codes) using the current code:

 FileUpload fileUpload = new FileUpload(); Button uploadButton = new Button("Click"); uploadButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { String filename = fileUpload.getFilename(); Window.alert(filename); } }); 
+1
source share
  • You cannot get the absolute file path on the user device from the GWT FileUpload widget.

  • You do not need an absolute path to the file to download and store it as a byte array.

The GWT documentation provides an example of using the Upload File widget:

http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/FileUpload.html

+1
source share

All Articles