Extjs File Upload

Source: Internet
Author: User

Functional requirements:

Add a button on the mastplan page to upload an excel file and save the file content to the database before saving it.

 

Specific implementation:

 

Pai_^ first.

 
 

 

 
 

Jsp page code:

Add a button in the tbar toolbar: click this button to bring up a file upload window.

 

[Java]
, {Id: "upload_excel", xtype: "button", cls: "x-btn-icon", icon: "/_ static/icon/upload_excel.png", tooltip: "upload", scope: this, handler: function () {win_upload.show ();}}
 
 
Var win_upload = new Ext. Window ({
Title: 'uploadfile ',
Width: 450,
Height: 180,
Modal: true,
X: 100,
Y: 50,
Layout: 'form ',
AutoScroll: true,
Constrain: true,
BodyStyle: 'padding: 10px 10px 10px 10px ;',
Items: form,
Buttons :[{
Text: 'Confirm upgrade ',
Handler: function (){
If (form. form. isValid ()){
If (Ext. getCmp ('userfile'). getValue () = ''){
Ext. Msg. alert ('xin Xin hint ', 'select the file to be upgrade ');
Return;
}
Ext. MessageBox. show ({
Title: 'Please wait ....',
Msg: 'file in progress ....',
ProgressText :'',
Width: 300,
Progress: true,
Closable: false,
AnimEl: 'loding'
});
Form. getForm (). submit ({
Url: 'upload ',
Method: 'post ',
Success: function (form, action ){
Ext. Msg. alert ('success', 'Congratulations! File success! '+ Action. result. success );
Win_upload.hide ();
},
Failure: function (form, action ){
Ext. Msg. alert ('token failed', "the token is missing in the file. Please try again! ");
}
})
}
}
},{
Text: 'close ',
Handler: function (){
Win_upload.hide ();
}
}],
Closable: false,
Draggable: false,
Resizable: false
});
 
 
Java code:
Obtain and read the stream information before saving the file.
Obtain excel content
 
Package javaservlets. production. uploadexcel;
 
Import java. io. IOException;
Import java. io. InputStream;
Import java. util. List;
 
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Import jxl. Cell;
Import jxl. Sheet;
Import jxl. Workbook;
Import jxl. read. biff. BiffException;
 
Import org. apache. commons. fileupload. FileItem;
Import org. apache. commons. fileupload. FileUploadException;
Import org. apache. commons. fileupload. disk. DiskFileItemFactory;
Import org. apache. commons. fileupload. servlet. ServletFileUpload;
Import org. json. JSONException;
Import org. json. JSONObject;
 
Public class Upload extends HttpServlet {
 
Private static final long serialVersionUID = 6777910910008132796l;
 
@ Override
Protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Super. doGet (req, resp );
System. out. println ("doGet ");
Resp. sendRedirect ("/upload/index. jsp ");
}

@ Override
Protected void doPost (HttpServletRequest req, HttpServletResponse resp ){

System. out. println ("----------------------------------- upload ---------------------------------------------------");

InputStream is = null;
Workbook workbook = null;

Try {
List <FileItem> items = new ServletFileUpload (new DiskFileItemFactory (). parseRequest (req );
For (FileItem item: items ){
If (item. isFormField ()){
System. out. println (item. getFieldName ());
System. out. println (item. getString ());
} Else {
System. out. println (item. getFieldName ());
Is = item. getInputStream ();
Workbook = Workbook. getWorkbook (is );
}
}

Sheet [] sheetNum = workbook. getSheets ();
System. out. println ("---------------------------------- print sheet data:" + sheetNum. length + "----------------------------");
Sheet sheet = workbook. getSheet (0 );
Cell cell = null;
 
Int columnCount = sheet. getColumns ();
Int rowCount = sheet. getRows ();
For (int I = 0; I <rowCount; I ++ ){
For (int j = 0; j <columnCount; j ++ ){
Cell = sheet. getCell (j, I );
System. out. print (cell. getContents ());
}
System. out. println ("\ n ");
}
Resp. setContentType ("text/html ");
JSONObject jObject = new JSONObject ();
Try {
JObject. put ("success", "true ");
} Catch (JSONException e ){
E. printStackTrace ();
Try {
JObject. put ("success", "false ");
} Catch (JSONException e1 ){
E1.printStackTrace ();
}
}
Resp. getWriter (). print (jObject. toString ());
// Resp. sendRedirect ("/upload/index. jsp ");
Workbook. close ();
} Catch (FileUploadException e ){
E. printStackTrace ();
} Catch (BiffException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.