Problem with file upload

28 views
Skip to first unread message

balachandra maddina

unread,
Jun 16, 2010, 3:54:51 AM6/16/10
to google-we...@googlegroups.com
Hi There,

  Im trying a file upload using the below code but on my server side no file items are available but without changing the server code if i try using a plain html with a form then the server was able to see file items. please let me know if im missing anything here.

GWT code:
-----------------
 uploadForm = new FormPanel();
      uploadForm.reset();
      uploadForm.setAction("../upload");
      uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
      uploadForm.setMethod(FormPanel.METHOD_POST);

      FileUpload upload = new FileUpload();
      uploadForm.add(upload);
      uploadPanel.add(uploadForm);

Server side code:
-------------------------
String contentType = request.getContentType();
      if (!ServletFileUpload.isMultipartContent(request)) {
         return super.onSubmit(request, response, command, errors);
      }
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      try {
         List<FileItem> fileItems = upload.parseRequest(request);
         if (null != fileItems && !fileItems.isEmpty()) {


Your help is very much appreciated.

Thank you,
bala. 

aditya sanas

unread,
Jun 16, 2010, 4:24:06 AM6/16/10
to google-we...@googlegroups.com
try adding a statement upload.setName("<your form name>");
after creation of object for FileUpload.
--
Aditya


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Bruno Lopes

unread,
Jun 16, 2010, 4:24:33 AM6/16/10
to google-we...@googlegroups.com
Hellos balachandra ! :D

I create this solution and it  works fine:

Cliente side:

final FormPanel form = new FormPanel();

final FileUpload upload = new FileUpload();

public static  String UPLOAD_RESULT = "";

public static String EVENT_NAME = "";

private boolean isIMG = true;


public FileUploadPadroes(){

form.setEncoding(FormPanel.ENCODING_MULTIPART);

form.setMethod(FormPanel.METHOD_POST);


HorizontalPanel holder = new HorizontalPanel();


upload.setName("upload");

holder.add(upload);

upload.addChangeHandler(new ChangeHandler() {

@Override

public void onChange(ChangeEvent event) {


form.setAction("/padroes/UploadService?isIMG="+isIMG()+"&eventName="+EVENT_NAME);


if(upload.getFilename()==null || upload.getFilename().length()<1){

// PageBusAdapter.publishDoCreateEventUpload("No valid file attached.");

}else

form.submit();

}

}

});

form.add(holder);

}


public FormPanel getForm() {

return form;

}

public FileUpload getUpload() {

return upload;

}


public boolean isIMG() {

return isIMG;

}


public void setIMG(boolean isIMG) {

this.isIMG = isIMG;

}


On server side :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


// ServletFileUpload uploadHandler = new ServletFileUpload();

boolean isIMG = Boolean.parseBoolean(request.getParameter("isIMG"));

// Create a factory for disk-based file items

DiskFileItemFactory factory = new DiskFileItemFactory();


// Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload(factory);


if(isIMG){

// Set factory constraints

factory.setSizeThreshold(1000000);//"resources/upload"

// Set overall request size constraint

upload.setSizeMax(1000000);

}else {

// Set factory constraints

factory.setSizeThreshold(10000000);//"resources/upload"

// Set overall request size constraint

upload.setSizeMax(10000000);

}



try {

/*

* Parse the request

*/

String eventName = request.getParameter("eventName");

List<FileItem> lUploadingFiles = upload.parseRequest(request);

for (FileItem item : lUploadingFiles) {


processUpload(item,isIMG,eventName);

}


}catch(FileUploadException ex) {

FileUploadPadroes.UPLOAD_RESULT = "The file size is to big," +

" must be less than 1MB.";

log("Error encountered while parsing the request",ex);

} catch(Exception ex) {

log("Error encountered while uploading file",ex);

}

}


private void processUpload(FileItem uploadItem,boolean isIMG,String eventName)

{

File uploadedFile = null;

String uploadFileName = uploadItem.getName();

 

if(isIMG)

uploadedFile = new File("padroes/resources/event/"+ eventName +"/images/" + uploadFileName);

else

uploadedFile = new File("padroes/resources/event/"+ eventName +"/videos/" + uploadFileName);

/**

    * handling file uploads

    */

      try

  {

uploadItem.write(uploadedFile);


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}




--

malliseven.hills

unread,
Jun 16, 2010, 4:28:15 AM6/16/10
to google-we...@googlegroups.com
uploadForm = new FormPanel();
      uploadForm.reset();
      uploadForm.setAction("upload");
      uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
      uploadForm.setMethod(FormPanel.METHOD_POST);

      FileUpload upload = new FileUpload();
      uploadForm.add(upload);
      uploadPanel.add(uploadForm);




--

balachandra maddina

unread,
Jun 16, 2010, 4:50:01 AM6/16/10
to google-we...@googlegroups.com
Thank you. its working fine after setName.

Regards,
bala.  
Reply all
Reply to author
Forward
0 new messages