views:

3515

answers:

2

I am trying to do a file upload from gwt-ext without bringing up the dialog box. To do this, I created a FormPanel and added the appropriate fields to it. Then did a form.submit(). This doesn't seem to work. Any idea why? The code is shown below.

final FormPanel uploadForm = new FormPanel();
uploadForm.setVisible(false);
uploadForm.setFileUpload(true);
final TextField sourceFile = new TextField("File", "sourceFile");
sourceFile.setVisible(false);
sourceFile.setInputType("file");
sourceFile.setValue("/tmp/test.txt");

final TextField targetFile = new TextField("Upload As", "targetFile");
targetFile.setVisible(false);
targetFile.setValue("different.txt");

uploadForm.add(sourceFile);
uploadForm.add(targetFile);

final String url = GWT.getModuleBaseURL() + "/uploadFile";
uploadForm.getForm().submit(url, null, Connection.POST, null, false);

I tested the servlet on the server side with a simple html form and it works correctly. Only the GWT-EXT version doesn't seem to work.

A: 

I found out why the above piece of code is not working. The primary issue here is that file uploads are blocked by the browser due to security reasons if the upload form has not been rendered and/or if the form has been modified after the user clicks the submit button. If the browser did allow such things, then any file on the system can be easily uploaded without the user's knowledge.

Solution to to the above problem is to bring up the dialog box, do the upload in the event handler for the submit button and in the onActionComplete method of the form listener, do any other processing.

Bala
A: 

The whole idea of uploading without dialog box looks like a security breach to me. I can imagine an application that steals passwords file whenever opened, if only the above would be possible.

ciukes
The motivation for doing that was to keep the window alive while the upload happens in the background (like the flickr photo upload). I found a different solution to the underlying problem so that I didn't have to do background file upload.
Bala
could you share the solution you've found?
ciukes

related questions