Hello,
I am writing a GWT application based on the 'Showcase' sample (http://
gwt.google.com/samples/Showcase/Showcase.html). I am trying to get
the 'File Upload' feature to work. I have changed the 'CwFileUpload'
class as follows:
public Widget onInitialize() {
// Create a FormPanel and point it at a service.
final FormPanel form = new FormPanel();
form.setAction(GWT.getModuleBaseURL() + "myFormHandler");
// Because we're going to add a FileUpload widget, we'll need to
set the
// form to use the POST method, and multipart MIME encoding.
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// Create a vertical panel to align the content
VerticalPanel vPanel = new VerticalPanel();
form.setWidget(vPanel);
// Add a label
vPanel.add(new HTML(constants.cwFileUploadSelectFile()));
// Add a file upload widget
final FileUpload fileUpload = new FileUpload();
fileUpload.ensureDebugId("cwFileUpload");
vPanel.add(fileUpload);
// Add a button to upload the file
Button uploadButton = new Button(constants.cwFileUploadButton());
uploadButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String filename = fileUpload.getFilename();
if (filename.length() == 0) {
Window.alert(constants.cwFileUploadNoFileError());
} else {
// Window.alert(constants.cwFileUploadSuccessful());
form.submit();
}
}
});
vPanel.add(new HTML("<br>"));
vPanel.add(uploadButton);
// Add an event handler to the form.
form.addSubmitHandler(new SubmitHandler() {
@Override
public void onSubmit(SubmitEvent event) {
if (fileUpload.getFilename().length() == 0) {
Window.alert("Must select a valid file");
event.cancel();
}
}
});
form.addSubmitCompleteHandler(new
FormPanel.SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
// When the form submission is successfully completed, this
// event is fired. Assuming the service returned a response of
type
// text/html, we can get the result text here (see the
FormPanel
// documentation for further explanation).
Window.alert(event.getResults());
}
});
RootPanel.get().add(form);
// Return the layout panel
return vPanel;
}
The code in MyFormHandler is given below (Note: This was directly
copied from the Google Appengine FAQ page):
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
ServletFileUpload upload = new ServletFileUpload();
res.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
// IT NEVER GETS IN THIS LOOP
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
LOG.warning("Got a form field: " + item.getFieldName());
} else {
LOG.warning("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
// You now have the filename (item.getName() and the
// contents (which you can read from stream). Here we just
// print them back out to the servlet output stream, but you
// will probably want to do something more interesting (for
// example, wrap them in a Blob and commit them to the
// datastore).
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1)
{
res.getOutputStream().write(buffer, 0, len);
}
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
When form is submitted, no "FileItems" (meaning form fields) are
returned in the Request object. I have tried moving the FormPanel to
Showcase.java and tried several combinations, but nothing is working :
( Any ideas on debugging this further will be greatly appreciated.
Please help.
Thank you.
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.