Ext文件上传、下载
* Ext上传:
ext文件上传需要扩展控件的支持:
<form id="uploadFileForm" action="" method="post" enctype="multipart/form-data">
??? <div id="fileupload" style="cursor:hand;"></div>
</form>
1 前台js代码
var uploadForm = new Ext.BasicForm('uploadFileForm');
var fileUploadBt = new Ext.ux.form.FileUploadField({
??? renderTo: 'fileupload',??? ???
??? buttonOnly: true,
??? buttonText: 'Excel导入',
??? buttonCfg: {
??? ??? iconCls: 'download'
??? ??? //tooltip: '从Excel导入物料信息'
??? },
??? listeners: {
??? ??? 'fileselected': function(fb, v){
??? ??? //v 为文件路径
??? ??? if(fac.getValue() == ''){
??? ??? ??? Ext.Msg.alert('操作提示', '请先选择一个成本中心或填写内部订单号!');
??? ??? ??? fb.reset();
??? ??? }else{
??? ??? ??? indexFun.addMarcFromExcel(v);
??? ??? }
??? ??? }
??? }
});
uploadForm.add(fileUploadBt);
addMarcFromExcel :function(filePath){
??? uploadForm.submit({
??? ??? fileUpload: true,
??? ??? standardSubmit: true,
??? ??? url: '<c:out value="${path}" />/purchaseApply.htm?method=parseExcel',
??? ??? params: {
??? ??? costCenter: costCenter.getValue(),
??? ??? factNo: costCenterCmbStore.getAt(costCenterCmbStore.find('costNo',costCenter.getValue())).get('factNo'),
??? ??? internalOrder: internalOrder.getValue()
??? ??? },
??? ??? waitMsg: '文件上传中,请等待...',???
??? ??? waitTitle: '上传进度条',
??? ??? success: function(form,action){??? ??? ???
??? ??? yyExt.info('导入成功', msgContent);
??? ??? for(var i=0;i<action.result.prdArr.length;i++){
??? ??? ??? indexFun.insertMarcToGrid(action.result.prdArr[i]);
??? ??? }
??? ??? form.reset();
??? ??? },
??? ??? failure: function(form, action) {
??? ??? form.reset();
??? ??? switch (action.failureType) {
??? ??? ??? case Ext.form.Action.CLIENT_INVALID:
??? ??? ??? Ext.Msg.alert('导入失败', 'Form fields may not be submitted with invalid values');
??? ??? ??? break;
??? ??? ??? case Ext.form.Action.CONNECT_FAILURE:
??? ??? ??? Ext.Msg.alert('导入失败', 'Ajax communication failed');
??? ??? ??? break;
??? ??? ??? case Ext.form.Action.SERVER_INVALID:
??? ??? ?????? Ext.Msg.alert('导入失败', action.result.msg);
??? ?????? }
??? ??? }
??? });
}
2 后台java代码:
/**
?* org.apache.commons.fileupload.servlet.ServletFileUpload
?* 采用apache的ServletFileUpload解析request中的文件,并将文件相关信息封装进一个FileBean对象
?*/
FileUploadUtil fileTool = new FileUploadUtil();
fileTool.setServletFileUpload(this.getServletFileUpload());
fileTool.setSizeMax(this.getFileMaxSize());
fileTool.init(request);
fileTool.setSaveFile(false); //上传的文件不保存到硬盘
FileBean fileBean = fileTool.saveFile();
/*
?* 提取其他参数
?*/
String costCenter = fileTool.getParameter("costCenter"); //成本中心
String factNo = fileTool.getParameter("factNo"); //工厂编号
String internalOrder = fileTool.getParameter("internalOrder"); //内部订单
* Ext下载:Ext没有对下载做任何处理,下载采用一般方式即可。
采用form提交方式:
<form? id="queryForm" target="myframe" method="post">
??? <input type="text" id="marcNo" name="marcNo" style="width: 180px;">
??? <input type="text" id="marcName" name="marcName" style="width: 100px">
</form>
//导出物料信息
download: function(){
??? $("queryForm").action = "<c:out value="${path}"/>/marcInfo.htm?method=download";
??? $("queryForm").submit();
}
后台java代码:
public ModelAndView download(HttpServletRequest request,HttpServletResponse response) throws Exception{
??? ??? response.setCharacterEncoding(this.DEFAULT_ENCODE);
??????? //取得输出流
??????? OutputStream out = response.getOutputStream();
??????? //清空输出流
??????? response.reset();
??? try {
??? /**
??? ?* 1 提取条件参数
??? ?*/
??? String marcNo = RequestUtil.getParameter(request, "marcNo", null); //物料编码
??? String marcName = RequestUtil.getParameter(request, "marcName", null); //品名
???
??? //设置响应头和下载保存的文件名
??? response.setHeader("Content-Type","text/html; charset=UTF-8");
??? response.setHeader("content-disposition","attachment;filename="+excelModel.getPath());
??? //定义输出类型
??? response.setContentType("APPLICATION/msexcel");?
??? /**
??? ?* 2 根据条件参数得到相关文件的输出流,将输出流写回前台
??? ?*/
??? } catch (Exception e) {
??? ??? log.error("下载物料信息出错:"+e.getMessage());
??? }finally{
??? ??? out.close();
??? ??? out.flush();
??? }
??? return new ModelAndView(this.indexView,"sessionUserName",super.getSessionUser(request).getUserName());
}