前台
用到的Ext组件
{ xtype: 'fileuploadfield', width:300, flex: 1, emptyText: '请选择文件', fieldLabel: '请选择文件', allowBlank: false, id: 'import', fileTypes:['xlsx','xls'], name: 'importFile', buttonText: '浏览', vtype:'fileType' }
此组件似乎没有校验文件后缀名的功能,需要自己校验,此处用xtype,如果用form的url提交,会显示自带的进度条,如果是用dwr异步提交,则不会显示了。
dwr异步提交的关键代码:
var fileValue=dwr.util.getValue("import-file");
vlanAdapter.uploadFiles(fileValue,function(data){
alert(data);
});
后台:
Adapter中方法的定义:
public String uploadFiles(FileTransfer file){ Workbook workbook=new HSSFWorkbook(file.getInputStream()); }
下载部分:
后台adapter方法:
public FileTransfer downLoadTemplate(){ try { // BufferedInputStream in = new BufferedInputStream(new FileInputStream("C:/Users/tanghui/Desktop/模版.xlsx")); // String filePath=realPath+"service/template/模板.xlsx"; String filePath = SystemRoot.getRootPathAsWebApp()+"service"+File.separator+"template"+File.separator+"模版.xls"; filePath = filePath.replaceAll("/", File.separator+File.separator); File file = new File(filePath); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } String filename="模版.xls"; return new FileTransfer(new String( filename.getBytes("GBK"),"iso8859-1" ), "application/vnd.ms-excel", out.toByteArray()); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
前台js
vlanAdapter.downLoadVlan(deviceName,ponName,cardId,{callback :function(data){
dwr.engine.openInDownload(data)
},async:false});