I am trying to use Upload component. My requirement are as following:
Display a button “Select File” which lets user select a file to upload but not browser file upload. Clicking over button “Select File” should display “Open” dialog.
After selecting a file, the file should not be uploaded immediately.
Provide a “Upload” button. Clicking on this button should upload the file selected by the user.
The Upload component can be used to show custom upload button instead of browser’s file upload element.
However, the custom upload button uploads the file immediately upon selection.
If the immediate flag is set to false then it displays browser’s file upload element.
Is there a way to display custom upload button and do not upload file immediately?
One possible but not beautiful way would be following. The problem with the following code is that the browser loading indicator runs until the “Upload it”-button is clicked. It may be possible to get more sophisticated solution using the Java reflection API…
mainWindow = new Window();
setMainWindow(mainWindow);
Button uploadButton = new Button("Upload it");
uploadButton.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
allowUpload = true;
}
});
HorizontalLayout hlo = new HorizontalLayout();
Upload upload = new Upload("Upload a file",new Upload.Receiver() {
public OutputStream receiveUpload(String filename, String MIMEType) {
while (!allowUpload) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
System.out.println("UPLOADING NOW...");
allowUpload = false;
return null;
}
});
upload.setImmediate(true);
upload.setButtonCaption("Select file...");
upload.addListener(new Upload.FinishedListener() {
public void uploadFinished(FinishedEvent event) {
System.out.println("UPLOADED");
}
});
hlo.addComponent(upload);
hlo.addComponent(uploadButton);
hlo.setComponentAlignment(uploadButton, Alignment.BOTTOM_LEFT);
mainWindow.addComponent(hlo);
As you said it works but the browser loading indicator keeps running as well as the browser’s progress bar displays a kind of progress. This is really very confusing to the application users.
When you say that using Java Reflection we can get even better solution. You mean we can get rid of the browser’s progress indicator.
Isn’t file upload a JavaScript thing?
Is there any reason why Vaadin does not provide lazy file upload?
I am porting my project from Vaadin 6.xx to Vaadin 7 (using 7.3.9). My code creates simple window containing Upload component in immediate mode. I also added another “Cancel” button used for closing window. So, I have window with two buttons: “Upload” and “Cancel” inserted in HorizontalLayout. My problem is when I click on “Cancel” button file choosing dialog appers, as if I click “Upload” button. The same code in Vaadin 6 works as expected. Can you help me?