extjs file upload and import and local template download

logisticLockSettingFormImport = Ext.extend(Ext.Window, {
    formPanel: null,
    constructor: function(a) {
        Ext.applyIf(this, a);
        this.initUIComponents(a.colData);
        logisticLockSettingFormImport.superclass.constructor.call(this, {
            id: "logisticLockSettingFormImportWin",
            layout: "fit",
            iconCls: "menu-icWorkPlan",
            items: this.formPanel,
            modal: true,
            autoHeight: true,
            width: 525,
            minWidth: 525,
            maximizable: false,
            resizable: false,
            title: "导入",
            buttonAlign: "center",
            buttons: this.buttons,
            keys: {
                key: Ext.EventObject.ENTER,
                fn: this.save.createCallback(this.formPanel, this),
                scope: this
            }
        });
    },
    initUIComponents: function(tb) {
        this.formPanel = new Ext.FormPanel({
            layout: "form",
            autoHeight: true,
            fileUpload:true,
            frame: false,
            labelAlign: "right",
            labelWidth: 40,
            defaults: {
                width: 500
            },
            border: false,
            url: __ctxPath + "/logic/logisIlockExcelImportLogicIlock.do",
            id: "logisticLockSettingFormImport",
            bodyStyle: "padding: 0px 5px 0px 5px",
            items: [{
                xtype: "fieldset",
                title: "智能锁信息导入",
                inputType : 'file',
                defaults: {
                    xtype: "textfield",
                    anchor: "100%,100%"
                },
                items: [{
                xtype : 'fileuploadfield',
                    emptyText : 'Select upload file',
                    fieldLabel : 'file',
                    name : 'uploadExcel',
                    id:'uploadExcel',
                    buttonText : 'Please select a file',
                    anchor : '95%',
                    allowBlank: false,
                    listeners : {
                        'fileselected' : function(fb, v) {
                var allowfiletype='. xls,.xlsx';//Formats that can only be imported
                            var temp = v.replace(/^.*(\.[^\.\?]*)\??.*$/, '$1');/ /Get the file extension
                            var temp1 = temp.toLocaleLowerCase();
                            //alert(temp1);
                            if (allowfiletype.indexOf(temp1) == -1) {
                                Ext.Msg.alert("Error","This type of file is not allowed to be selected, please select again!");
                                fb.setValue("");
                                FileRname.setValue("");
                            }
                        }
                    }
                },{
                xtype : ' label',
                html : ' <font color=red>Smart lock import template download</font> '
                }]
            }]
        });

        this.buttons = [{
            text: "Save",
            iconCls: "btn-save",
            handler : this.save.createCallback(this. formPanel, this)
        },
        /*{
text :"重置",
iconCls :"btn-reset",
handler :this.reset
.createCallback(this.formPanel)
},*/
        {
            text: "取消",
            iconCls: "btn-cancel",
            handler: this.cancel.createCallback(this)
        }];
    },
    reset: function(a) {
        a.getForm().reset();
    },
    cancel: function(a) {
        a.close();
    },
    save: function(a, b) {
        if (a.getForm().isValid()) {
//        var filenPath = Ext.getCmp("uploadExcel").getValue();
// filenPath=filenPath.replace(/\\/g, "\\\\");//Convert \ to \\
// alert("filenPath==="+filenPath);
// Ext.Ajax. request({
// url: __ctxPath + "/logic/logisIlockExcelImportLogicIlock.do?uploadExcel="+filenPath, //import interface
// success: function(d) {
// alert("=="+d.responseText);
// var g = Ext.util.JSON.decode(d.responseText);
// console.log(g);
// if(g.success==true){
// Ext.ux.Toast.msg(" Operation information", "Successfully imported information!"+g.data);
// var d = Ext.getCmp("logicIlockpanel");
// if (d != null) {
// d.getStore().reload( );
// }
// b.close();
// }else{
// Ext.MessageBox.show({
// title: "Operation information",
// msg: "Error saving information, please contact the administrator!"+g.data ,
// buttons: Ext.MessageBox.OK,
// icon: Ext.MessageBox.ERROR
// });
// b.close();
// }
// }
// });
            a.getForm(). submit({
                method: "POST",
                waitMsg: "Importing data...",
                success: function(c, e) {
                    Ext.ux.Toast.msg("Operation information", "Saving information successfully!");
                    var d = Ext.getCmp("logicIlockpanel");
                    if (d != null) {
                        d.getStore().reload();
                    }
                    b.close();
                },
                failure: function(c, d) {
                    Ext. MessageBox.show({
                        title: "Operation information",
                        msg: "Error saving information, please contact the administrator!",
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.ERROR
                    });
                    b.close();
                }
            });
        }
    }
});

/**
* Information import background code
* @return
* @throws Exception
*http://localhost:8080/HEMS/logic/logisIlockExcelImportLogicIlock.do
*/
public String logisIlockExcelImport() throws Exception{
HttpServletRequest req = ServletActionContext.getRequest ();
HttpServletResponse response = ServletActionContext.getResponse();
req.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("UTF-8") ;

//Get the physical path (absolute path) of the folder to be saved
        String realPath=ServletActionContext.getServletContext().getRealPath("/attachFiles/");
        File tempFile = new File(realPath);
        if(!tempFile.exists()){
        tempFile.mkdirs();
        }
       
List<String> list = null;
try {
//Save the file
FileUtils.copyFile(uploadExcel, new File(tempFile,uploadExcelFileName));
// String tempPath = F:\\logis_ilock.xlsx";
        //Get the imported file path
        String savePath=ServletActionContext.getServletContext().getRealPath("/attachFiles/"+this.uploadExcelFileName);
File importFile = new File(savePath);
            list = ExcelHelper.exportListFromExcel(importFile, 0);//Read excel and parse it according to your own needs,
            importFile.delete() is not provided here ;
           
String fnumber=null;//Number name
String fcode=null;//Lock code
String fphone=null ;//Phone number
String strLockSupplier=null;//Lock manufacturer
String fdescription=null;//Remark

String[] strs=null;//Content information of each line
LogisIlock lock = null;

StringBuffer buff=new StringBuffer("{\"success\":true ").append(",\"data\":\"");
for (String str : list) {
strs=str.split(",");//Test data, 863014530683340, 13526541534, Xing'an, test test test haha
​​fnumber=strs[0];
fcode=strs[1];
fphone=strs[2];
strLockSupplier=strs[3];
fdescription=strs[4];

lock = logicIlockService.getLogisIlock(fcode);
if(null !=lock){
if(buff.length()<25){
buff.append(fcode+"The lock code already exists");
}else{
buff.append(","+fcode+" The lock code already exists");
}
}else{
lock=new LogisIlock();
lock.setFnumber(fnumber);
lock.setFcode(fcode);
lock.setFphone(fphone);
lock.setLockSupplier(Integer.valueOf(DataDictionaryUtils.lockSupplier(strLockSupplier)));
lock.setFdescription(fdescription);
lock.setFstatus(0);
lock.setFisDel(0);
lock.setFcreatorId(ContextUtil.getCurrentUser().getUserId());
lock.setFdepId(ContextUtil.getCurrentUser().getDepartment()==null?null:ContextUtil.getCurrentUser().getDepartment().getDepId());
lock.setForgId(ContextUtil.getCurrentUser().getOrgId());
lock.setFcreateTime(new Date());
this.logicIlockService.save(lock);
if(buff.length()<25){
buff.append(fcode+"锁编码导入成功");
}else{
buff.append(","+fcode+"锁编码导入成功");
}
}
}
buff.append("\"}");
this.jsonString=buff.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326488893&siteId=291194637