PDA

View Full Version : Upload Max Size File and progress bar



B.Oury
17 Apr 2012, 7:02 AM
Hi,

I use a FormPanel to upload a file and cause I can't fix the maximum size of the file in the client, I calculate it in the servlet and throw a new javax.xml.ws.http.HTTPException(400) if the file's size is larger than 5000000.

The problem is that nothing happens.

There's some code.

In the client:



public class UploadPanel extends LayoutContainer {
private final Messages messages = GWT.create(Messages.class);
private MyFileUploadField myFileUploadField;
private HiddenField<Long> idTask = new HiddenField<Long>();
private HiddenField<Long> idPatient = new HiddenField<Long>();

@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
setStyleAttribute("margin", "10px");
final FormPanel panel = new FormPanel();
panel.setHeading(messages.ajouterUnFichier());
panel.setFrame(true);
panel.setAction("uploadFile");
panel.setEncoding(Encoding.MULTIPART);
panel.setMethod(Method.POST);
panel.setButtonAlign(HorizontalAlignment.CENTER);
panel.setWidth(350);

myFileUploadField = new MyFileUploadField();
myFileUploadField.setAllowBlank(false);
myFileUploadField.setShim(true);
myFileUploadField.setName("uploadedfile");
myFileUploadField.setFieldLabel(messages.fichier());
panel.add(myFileUploadField);

Button btn = new Button(messages.Save());
btn.addSelectionListener(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {
if (!panel.isValid()) {
return;
}
idPatient.setValue(5432L);
idTask.setValue(6789L);
panel.submit();
}
});

idPatient.setName("idPatient");
panel.add(idPatient);
idTask.setName("idTask");
panel.add(idTask);
panel.addButton(btn);

add(panel);
}
}

In the Servlet:



public class UploadServlet extends HttpServlet {
private File tmpDir;
private File destinationDir;

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");

DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setSizeThreshold(1 * 1024 * 1024); //1 MB
fileItemFactory.setRepository(tmpDir);

ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
uploadHandler.setSizeMax(5000000);
File file = null;

FileItem item = null;
try {

List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
item = (FileItem) itr.next();

if (item.isFormField()) {
out.println("File Name = " + item.getFieldName() + ", Value = " + item.getString());
} else {
file = new File(destinationDir, item.getName());
file.setWritable(true);
item.write(file);

OmniUser connectedUser = (OmniUser) request.getSession().getAttribute("USER");

String mimeType = this.getServletContext().getMimeType(file.getPath());
//TODO something
}
}
} catch (FileUploadException ex) {
log("Error encountered while parsing the request", ex);
} catch (Exception ex) {
if (ex instanceof HTTPException) {
log("Fichier trop volumineux", ex);
throw new HTTPException(400);
} else {
log("Error encountered while uploading file", ex);
}
} finally {
if (out != null) {
out.close();
}
if (item != null) {
item.delete();
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}





Must I redirect the page or something like that?

I ask me if it's possible to have a progress bar during the upload?

I use gxt 2.2.4 and gwt 2.3.0.

Thanks a lot for the help.

B.Oury
20 Apr 2012, 4:19 AM
No one has an idea? :((

sandeep.e
27 Aug 2012, 11:56 PM
In order to add a progress bar, you would need to process the servlet's response in client side. Then only you will be able to decide on when to stop the progress bar. In client side you can do some thing similar to this


FileUploadForm.this.addListener(Events.Submit, new Listener<FormEvent>() {


//EXT-FIX: public void onActionComplete(Form arg0, int arg1, String arg2) {
// }


public void handleEvent(FormEvent arg0) {
bar.stop();
bar.hide();
ClientUtils.alert("Result"+ arg0.getResultHtml());
}
});

And the bar.start before you call the panel.submit();

And in the servelet if you send a response back with the status of the request you will be able to check whether the request is processed successfully or not.
The file size you can check in servlet and you can send a response with an error

HTH
Sandeep