PDA

View Full Version : FileUpload + Excel + Java - Having response / event issues.



Erci
2 Nov 2010, 11:51 AM
I am trying to use Excel as input. The idea is to upload it to a Java Servlet. Grab additional data from the database and return new Excel to the user.
I have tried to do this many ways (with Flex and with extjs) and I can't seem to get it 100% right.
I am able to upload the file and process it AND generate new spreadsheet and return it, BUT I never get a response from Servlet back to extjs to tell it the processing is complete. So if I pop up a "wait" message, it never goes away. How can I tell extjs that Servlet completed processing?


var fp = new Ext.FormPanel({
//renderTo: 'fi-form',
//layout: 'absolute',
//standardSubmit: true,
//waitMsg: 'Uploading Your File...',
url: 'ExcelService',
fileUpload: true,
width: 450,
frame: true,
title: 'OPID Update',
autoHeight: true,
bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 120,
defaults: {
//anchor: '95%',
allowBlank: false
},
items: [{
xtype: 'fileuploadfield',
anchor: '100%',
id: 'form-file',
emptyText: 'Select a Spreadsheet',
fieldLabel: 'Input Spreadsheet',
name: 'photo-path' ,
buttonText: '',
buttonCfg: {
iconCls: 'upload-icon'
}
}],
buttons: [{
text: 'Process',
handler: function(){
if(fp.getForm().isValid()){
fp.getForm().submit({
waitMsg: 'Uploading Your File...',
success: function(fp, o){
msg('Success', 'File "'+o.result.file+'" uploaded to the server. Generating Updated Spreadsheet...');
},
failure: function(fp, o) {
Ext.Msg.alert('Failed', o.result.msg);
}
});
}
}
},{
text: 'Reset',
handler: function(){
fp.getForm().reset();
}
}]
});

And the Servlet:


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

try {
// Output to browser
out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
//response.setContentType("text/html");
response.setHeader("Content-Disposition", "attachment; filename=Network_Nbr_OPID_Updt.xls");
w = Workbook.createWorkbook(out);
s = w.createSheet("OPID_UPDATES", 0);

ServletInputStream is = request.getInputStream();
byte[] junk = new byte[1024];
@SuppressWarnings("unused")
int bytesRead = 0;

// the first four lines are request junk
bytesRead = is.readLine(junk, 0, junk.length);
bytesRead = is.readLine(junk, 0, junk.length);
bytesRead = is.readLine(junk, 0, junk.length);
bytesRead = is.readLine(junk, 0, junk.length);

// open and read uploaded xls
inBook = Workbook.getWorkbook(is);
inSheet = inBook.getSheet(0);
// end of read

// Create output header
s.addCell(new Label(0, 0, "PANEL_NUM"));
s.addCell(new Label(1, 0, "PROV_TYPE"));
s.addCell(new Label(2, 0, "PROV_NUM"));
s.addCell(new Label(3, 0, "TIN"));
s.addCell(new Label(4, 0, "ST"));
s.addCell(new Label(5, 0, "MKT"));
s.addCell(new Label(6, 0, "Contr"));
s.addCell(new Label(7, 0, "ConMast"));
s.addCell(new Label(8, 0, "DOC_ID"));
s.addCell(new Label(9, 0, "OPID"));
s.addCell(new Label(10, 0, "OFFICE"));
s.addCell(new Label(11, 0, "Date_Stamp"));
// end of header

// add data from input
// Row Count
int rows = inSheet.getRows();

for (int i = 1; i < rows; i++){
s.addCell(new Label(0, i, inSheet.getCell(0,i).getContents().toString().trim()));
s.addCell(new Label(1, i, inSheet.getCell(3,i).getContents().toString().trim()));
s.addCell(new Label(2, i, inSheet.getCell(4,i).getContents().toString().trim()));
s.addCell(new Label(3, i, inSheet.getCell(5,i).getContents().toString().trim()));
s.addCell(new Label(4, i, inSheet.getCell(6,i).getContents().toString().trim()));
s.addCell(new Label(5, i, inSheet.getCell(7,i).getContents().toString().trim()));
s.addCell(new Label(6, i, inSheet.getCell(8,i).getContents().toString().trim()));
s.addCell(new Label(7, i, inSheet.getCell(9,i).getContents().toString().trim()));
// end of NDB calls
}
// end add data from input
w.write();
w.close();
inBook.close();
out.flush();

out.close();
// try to send additional info
//response.reset();
//response.setContentType("text/html");
response.getOutputStream().close();
// end of output

} catch (Exception e) {
doError(e.getLocalizedMessage());
return;
}
System.out.println("Excel should have gotten popped up with results");
}


I also tried to return data as JSON. Figured I would use that to populate grid and then create extract to CSV toolbar button (can't use Excel extract utility.. company is still using IE6 : ).

This almost works.. I can return the data AND I get a response from Servlet, but here's the odd part. It returns as "failure" with status == undefined. I can post that code too, if someone wants to take a look at it.

Condor
3 Nov 2010, 4:36 AM
That is the problem with Content-disposition:attachment. Ext never gets signalled when the server has send the xls back.

I'm currently doing a standardSubmit:true to target="_blank" instead.

That way you don't get a wait message, but it does pop up a new window or tab with a progress indicator.

Erci
3 Nov 2010, 8:39 AM
That's pretty much what I thought. I also thought about popping up a new window through Flex, but was hoping to avoid it.
I'll give it a try. Tx very much for you reply.

michaelyang
1 Dec 2010, 7:28 PM
Hi Condor
i have a problem for ext js.
The problem is i create a new window and it has two items for gridpanel, one's id is panel1, but now i want get the gridpanel'id ,how can i do it?
thanks!

Erci
2 Dec 2010, 4:35 AM
Hi Condor
i have a problem for ext js.
The problem is i create a new window and it has two items for gridpanel, one's id is panel1, but now i want get the gridpanel'id ,how can i do it?
thanks!

What does this have to do with the original post?