Reputation: 8640
i created widget, and i added directly to root panel
public class FileUploadWidget extends VerticalPanel {
public FileUploadWidget() {
super();
final FormPanel form = new FormPanel();
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setAction("/FileUpload");
form.setWidget(this);
final FileUpload fileUpload = new FileUpload();
fileUpload.setName("uploadFormElement");
this.add(fileUpload);
Label maxUpload = new Label();
maxUpload.setText("Maximum upload file size: 1MB");
this.add(maxUpload);
Button button = new Button("Submit");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit();
}
});
this.add(button);
form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
Window.alert(event.getResults());
}
});
}
}
i created servlet
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
FileItem uploadItem = getFileItem(request); //BREAKPOINT
if (uploadItem == null) {
response.getWriter().write("NO-SCRIPT-DATA");
return;
}
try {
uploadItem.write(new File("newfile.txt"));
} catch (Exception e) {
response.getWriter().write("ERROR");
}
response.getWriter().write("OK");
}
private FileItem getFileItem(HttpServletRequest request) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()
&& "uploadFormElement".equals(item.getFieldName())) {
return item;
}
}
} catch (FileUploadException e) {
return null;
}
return null;
}
}
i put to my web.xml file mapping for servlet
<servlet>
<servlet-name>FileUpload</servlet-name>
<servlet-class>my.web.app.GwtFileUpload.server.FileUploadServlet</servlet-class>
</servlet>
....
<servlet-mapping>
<servlet-name>FileUpload</servlet-name>
<url-pattern>/FileUpload</url-pattern>
</servlet-mapping>
now when i select file, and call submit, when i'm using firefox nothing really happened (all what i got is SubmitEvent), my breakpoint, which i marked in servlet code is never reached
when i try same code under chrome, it is comming into servlet, but it didn't find any file
can anyone explain me why there is so different behaviour, and how i can fix it?
Upvotes: 0
Views: 613
Reputation: 7630
Add fileUpload control into FormPanel
, because The FileUpload
widget
(those that implement HasName) will be submitted to the server if they are contained within this panel.
You are added fileupload
into verticlepanel after doing form.setWidget(this);
. do like
this.add(fileUpload);
form.setWidget(this);
Upvotes: 2
Reputation: 9741
I suggest to use the gwtupload library, it would save a lot of time writing your own code and would provide many features to your app like customize buttons, progress bar, file extensions, size limit, etc.
You have this GettingStarted and this example page.
Upvotes: 0
Reputation: 2524
The FileUpload
widget wraps the HTML
element. This widget must be used with FormPanel
if it is to be submitted to a server.
Thus it is necessary to add fileupload to the FormPanel
. You have added it to directly the Panel
.
Please replace the code
this.add(fileUpload);
to
form.add(fileUpload);
For more details please go thorough the link http://www.tutorialspoint.com/gwt/gwt_fileupload_widget.htm
Upvotes: 3