Thomas Lefort
unread,Nov 12, 2012, 7:11:47 AM11/12/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to google-we...@googlegroups.com
I implemented a simple image uploader which relies on the form panel. I pass an image file via a filupload widget and a multipart encoding to the server which then processes the image, stores it on the disk and returns the image file path as a text/html response. On the client side I have an onSubmitCompleted event handler that sets the image widget url to the one returned by the servlet.
The onSubmitCompleted handler is sometimes (actually quite often) not called. I checked with firebug, the servlet does return the right file path, the image is stored, etc... The servlet also gets fully executed with no error (checked with various traces). I have the behavior for (at least) FF16. I have the issue in hosted mode or in compiled ran locally (with the embedded jetty).
Here's my servlet code:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
try {
// default values for width and height
int width = 50;
int height = 50;
InputStream filecontent = null;
String saveFile = null;
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
String fieldName = item.getFieldName();
String fieldValue = item.getString();
if(fieldName.equalsIgnoreCase("width")) {
width = Integer.parseInt(fieldValue);
}
if(fieldName.equalsIgnoreCase("height")) {
height = Integer.parseInt(fieldValue);
}
} else {
String fieldName = item.getFieldName();
saveFile = FilenameUtils.getName(item.getName());
if(fieldName.equalsIgnoreCase("image")) {
filecontent = item.getInputStream();
}
}
}
// check values
if(filecontent == null) {
throw new FileNotFoundException("no file provided");
}
// Process the input stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = filecontent.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
String imagePath = processAndStoreImage(out, saveFile, width, height);
System.out.println(imagePath);
// return the url of the file
writer.println(imagePath);
} catch (Exception e) {
writeError(writer, "Error whilst processing and storing image.");
} finally {
writer.flush();
writer.close();
}
}
and the onSubmitHandler
imageForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
final String result = event.getResults();
// update thumbnail view with new image
thumbnailImage.setUrl(result);
Window.alert("YES");
}
});