上传文件
Posted chunhui521
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传文件相关的知识,希望对你有一定的参考价值。
<button class="btn btn-primary" type="button" onclick="openword()"><i class="fa fa-file-word-o" style="margin-right:5px;"></i>生成word</button>
<form id="xm_gtxmkpFile" enctype="multipart/form-data"> <div class="box box-primary"> <div class="box-header with-border clearfix"> <span class="box-title" style="line-height:30px;">项目资料</span> </div> <div class="box-body"> <div class="table-responsive"> <table class="table table-bordered table-striped table-hover table-condensed table-zl" style="margin-bottom:0;"> <tr> <td>营业执照</td> <td><div id="yyzzimageFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="yyzzimage" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘yyzzimage‘)">上传</button></td> </tr> <tr> <td>项目照片</td> <td><div id="xmzpimageFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="xmzpimage" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘xmzpimage‘)">上传</button></td> </tr> <tr> <td>项目概况</td> <td><div id="xmgkimageFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="xmgkimage" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘xmgkimage‘)">上传</button></td> </tr> <tr> <td>项目合同</td> <td><div id="xmhtimageFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="xmhtimage" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘xmhtimage‘)">上传</button></td> </tr> <tr> <td>项目备案</td> <td><div id="xmbaimageFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="xmbaimage" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘xmbaimage‘)">上传</button></td> </tr> <tr> <td>相关研究报告</td> <td><div id="xgyjbgimageFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="xgyjbgimage" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘xgyjbgimage‘)">上传</button></td> </tr> <tr> <td>是否房地产项目</td> <td colspan="3"> <select id="isfdc" class="form-control input-sm" style="width:150px;"> <option value="0">非房地产</option> <option value="1">房地产</option> </select> </td> </tr> <tr> <td>土地证</td> <td><div id="tdzimageFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="tdzimage" id="tdzimage" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘tdzimage‘)">上传</button></td> </tr> <tr> <td>项目其他材料</td> <td><div id="xmqtclFileDiv"></div></td> <td><input class="btn btn-primary btn-sm" type="file" name="xmqtcl" accept="image/*" /></td> <td><button class="btn btn-primary btn-sm" type="button" onclick="xmghxg(‘xmqtcl‘)">上传</button></td> </tr> <tr> <td colspan="4" class="text-center"> <button class="btn btn-primary" type="button" onclick="openword()"><i class="fa fa-file-word-o" style="margin-right:5px;"></i>生成word</button> </td> </tr> </table> </div> </div> </div> </form>
function xmghxg(type){ $("#xm_gtxmkpFile").ajaxSubmit({ "type":"post", // 提交方式 get/post "url" : "<c:url value =‘/bi/fillform/fillformword/xmSaveInfo‘/>", "data": { xmid:$("#xmid_").val(), paramBgq: ‘‘, fileName:‘file‘, type:type }, "success": function(data) { // data 保存提交后返回的数据,一般为 json 数据 var bhlMsg = new Bhl.frame.MessageBox().showMessage("上传成功!"); // 刷新文件列表 getGtxmkpFileList(type); // _page_gt_gtxmkp.updateGxsj();//更新维护时间 }, "error":function(){ var bhlMsg = new Bhl.frame.MessageBox().showMessage(‘上传失败‘,‘error‘,1000); bhlMsg.hide(); } }); }
@RequestMapping("/xmSaveInfo") public void xmSaveInfo(HttpServletRequest request, HttpServletResponse res) throws Exception { String creattime = sdf.format(new Date()); String xmid = request.getParameter("xmid"); String xmjj = request.getParameter("xmjj"); String fileName = request.getParameter("fileName"); String type = request.getParameter("type"); System.out.println(request.getParameter("xiaoguotu_input")); List<Map<String, String>> fileList = fillformordservice.fileUploadBatch(xmid, request,fileName,type); if(fileList.size()!=0){ for (Map<String, String> map : fileList) { Set<String> keySet = map.keySet(); for(String key : keySet){ HashMap<String, Object> tmap =new HashMap<String, Object>(); String str = map.get(key); // String xmpic = str.substring(str.indexOf("//"+type+""), str.length()); String tpath = str; fileName = key; tmap.put("XMID_", xmid); tmap.put(type, tpath); tmap.put("FILENAME_", key); fillformordservice.saveOrUpXmInfo("BI_FILLFORM_WORD", tmap,type); } } }else{ HashMap<String, Object> tmap =new HashMap<String, Object>(); tmap.put("XMID_", xmid); tmap.put("XMJJ_", StringUtil.isEmpty(xmjj)?" ":xmjj); tmap.put("CREATTIME_", creattime); // gtxmkpService.saveOrUpXmInfo("BD_GTXMKP_XM_JIBEN", tmap,null); } }
public synchronized List<Map<String,String>> fileUploadBatch(String typeId, HttpServletRequest request,String fileName,String type) throws BusinessException { MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request; //String time = DateHelper.getSimpleFormatedDateNow(); Map<String,MultipartFile> fileMap = multipart.getFileMap(); List<Map<String,String>> fileList = new ArrayList<Map<String,String>>(); for(Entry<String,MultipartFile> entry:fileMap.entrySet()){ MultipartFile file = entry.getValue(); String fileKey = entry.getKey(); // 不同文件分包上传 String filePath = ProjectPathConfigHelper.getPath("gtxmkpFilePath") + "/pj-tybxg/" +fileKey+"/"+ typeId+"/" ; // d://hoihih/jij/132498713 String path="/pj-tybxg/" +fileKey+"/"+ typeId+"/"; MyFileUtils.mkDirectory(filePath); fileName = file.getOriginalFilename(); if(fileKey.equals(type)){ if(file.getSize() > 20971520){//10485760 throw new BusinessException("上传的["+fileName+"]附件超出20MB容量,请重新上传"); } if(StringUtils.isBlank(fileName)) continue; boolean b = MyFileUtils.isFileExist(fileName, filePath); if (b) { MyFileUtils.deleteFile(filePath+fileName); } boolean flag = MyFileUtils.upload(fileName, filePath, file); if(flag==false) throw new BusinessException("上传文件失败"); Map<String,String> fileInfo = new HashMap<String, String>(); fileInfo.put(fileKey,path+fileName); fileList.add(fileInfo); } } return fileList; }
function getGtxmkpFileList(type){ var _url = "<c:url value=‘/bi/fillform/fillformword/getFileListByType‘/>"; $.ajax({ type:‘POST‘, data:{type:type,xmid:‘${map.id_}‘}, url:_url, success:function(e){ debugger; var json = eval("{"+e+"}"); $("#"+type+"FileDiv").html(""); var div_a_id="div_a_id"; for(var i in json){ var pic = ‘‘; if(null!=json[i].YYZZIMAGE){ pic=json[i].YYZZIMAGE; }else if(null!=json[i].XMZPIMAGE){ pic=json[i].XMZPIMAGE; }else if(null!=json[i].XMGKIMAGE){ pic=json[i].XMGKIMAGE; }else if(null!=json[i].XMHTIMAGE){ pic=json[i].XMHTIMAGE; }else if(null!=json[i].XMBAIMAGE){ pic=json[i].XMBAIMAGE; }else if(null!=json[i].XGYJBGIMAGE){ pic=json[i].XGYJBGIMAGE; }else if(null!=json[i].TDZIMAGE){ pic=json[i].TDZIMAGE; }else if(null!=json[i].XMQTCL){ pic=json[i].XMQTCL; } var image=pic.split(","); var id = ‘${map.id_}‘; for(var i=0;i<image.length;i++){ var html = image[i].substring(image[i].lastIndexOf("/")+1,image[i].length); var isImg = false; for(var n in imgName){ if(html.indexOf(imgName[n]) > 0){ isImg = true; } } div_a_id="div_a_id"+i; if(isImg){ $("#"+type+"FileDiv").append(‘<div class="btn-fj">‘+ ‘<button id="‘+div_a_id+‘" type="button" class="btn btn-default" path="‘+image[i]+‘" onclick="_page_gt_gtxmkp.downloadGxFile($(this)) ">‘+html+‘</button>‘+ ‘<span class="glyphicon glyphicon-remove" path="‘+ image[i] +‘" onclick="deleteModea(‘‘+image[i]+‘‘,‘‘+id+‘‘,‘‘+type+‘‘)"></span>‘+ ‘</div>‘ ); /* $("#"+type+"FileDiv").append("<a id=‘"+div_a_id+"‘ class=‘btn btn-default pull-left m_5‘ href=‘#‘ path=‘"+pic+"‘ onclick=‘_page_gt_gtxmkp.downloadGxFile($(this))‘>"+html+"</a><button class=‘btn btn-small btn-danger‘ path=‘" + pic + "‘ type=‘button‘ onclick=‘_page_gt_gtxmkp.delImgXGT($(this),"+div_a_id+")‘>删除</button>"); */ } } } } });
以上是关于上传文件的主要内容,如果未能解决你的问题,请参考以下文章
ajaxFileUpload上传带参数文件及JS验证文件大小