PDA

View Full Version : Send response to client after reading file using FileUploadField



ravi.m183
31 Jan 2010, 9:59 PM
Hi,
I am using Gxt2.1 for my application development. In one of my modules, I need to read an excel file (using upload facility) and send the contents of the same to a Grid. To upload and read the excel file I am using FileUploadField of Gxt. The FileUploadField is added to a FormPanel which is submitted to a servlet. My client side code is as follows:



FormPanel formPanel = new FormPanel();
formPanel.setFrame(false);
formPanel.setBorders(false);
formPanel.setHeaderVisible(false);
formPanel.setAction(GWT.getModuleBaseURL() + "excel-upload-service");
formPanel.setEncoding(Encoding.MULTIPART);
formPanel.setMethod(Method.POST);
formPanel.setButtonAlign(HorizontalAlignment.CENTER);
formPanel.setWidth(350);

FileUploadField uploadField = new FileUploadField();
uploadField.setWidth(200);
uploadField.setFieldLabel("File");

Button uploadButton = new Button("Upload", null);
uploadButton.addListener(Events.Select,new Listener<ButtonEvent>(){
public void handleEvent(ButtonEvent be) {
formPanel.submit();
}
});

formPanel.add(uploadField);
formPanel.add(uploadButton);
My servlet doPost() method which reads the excel file content is as follows:



public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

try
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(req);
/* This method reads the excel contents and stores it in a customised object */
readExcelFile(items);
/* I want to send this object back to client side so that it can be used in a Grid */

}
catch (FileUploadException e) {
e.printStackTrace();
}
}
The problemt I am facing is that, I am storing the contents of the excel file in a serialized object which I want to send back to client side so that it can be used to display the contents in a Grid. But i dont know how to go about doing this.

Any help would be appreciated.

Thanks
Ravi

Arno.Nyhm
1 Feb 2010, 8:58 AM
one idea is to store the data on the server and then make a new request from the client to load it?

to entertain the user you can make an additional dialogbox that he need to confirm the upload or something like this

terciofilho
1 Feb 2010, 10:29 AM
Dude, the easiest way to do it is to make a second trip, by a RPC call.

Just mask the UI after the form has been posted and be happy. :-P

appamail@hotmail.com
2 Feb 2010, 2:55 AM
Try:



form.addListener(Events.Submit, new Listener<FormEvent>(){
public void handleEvent(FormEvent be) {
be.getResultHtml(); // Here is what you response sent.
}
}