form 表单提交数据和文件(fromdata的使用方法)
<!-- 数据和文件一次性提交 --> <form class="form_meren" id="mainForm" name="mainForm" action="${ctx}/shahescenicinfo/insertShaheScenicinfo.do" method="post" enctype="multipart/form-data"> <input class="form-control " id="aname" name="aname" type="text"> <input type="file" id="otherfiles">
<input type="file" id="otherfiles2">
<button type="button" onclick="sbutripform()" >保存</button> </form>
//验证结束后执行保存 function sbutripform(){ //form的id就是mainForm var mainstr=formToJsonObject(mainForm); //from表单对象 转json mainstr=JsonToStr(mainstr); mainstr=mainstr.replace("%", "%25"); var manstr = mainstr.toString(); var formData = new FormData(); formData.append("file1",document.getElementById("otherfiles").files[0]); formData.append("file2",document.getElementById("otherfiles2").files[0]); formData.append("jsonStr",manstr); $.ajax({ url:url, type:'POST', dataType:'json', data:formData, cache: false,//上传文件无需缓存 processData: false,//用于对data参数进行序列化处理 这里必须false contentType: false, //必须 success:function(xclyid){ window.top.customAlertTip("", "保存成功", "warning"); }, error:function (error){ window.top.customAlertTip("", "保存失败", "warning"); } }); }
@RequestMapping(value="/updateallareainfo.do", method={RequestMethod.GET,RequestMethod.POST}) @ResponseBody public String updateallareainfo(HttpServletRequest request, @RequestParam(value="file1",required=false)MultipartFile file1, @RequestParam(value="file2",required=false)MultipartFile file2, String jsonStr) { //把数据从json 转换为 实体类 YbsjAreaInfo ybsjAreaInfo = JSON.parseObject(jsonStr,YbsjAreaInfo.class); try { log.info("当前访问的action方法:updateallareainfo") ; // uploads 文件夹位置 String rootPath = request.getSession().getServletContext().getRealPath(PropertyManager.getProperty("voiceupload")); // uploads 文件夹2位置 String imgPath = request.getSession().getServletContext().getRealPath(PropertyManager.getProperty("filesavepath // 原始名称 String newFileName1 = ""; String newFileName2 = ""; if (file1!=null) { String originalFileName1 = file1.getOriginalFilename();//旧的文件名(用户上传的文件名称) //新的文件名 newFileName1 = UUIDUtil.getuuid() + originalFileName1.substring(originalFileName1.lastIndexOf(".")); File newFile = new File(rootPath + File.separator + newFileName1); // 判断目标文件所在目录是否存在 if( !newFile.getParentFile().exists()) { // 如果目标文件所在的目录不存在,则创建父目录 newFile.getParentFile().mkdirs(); } //存入 file1.transferTo(newFile); ybsjAreaInfo.setMapUrl("/"+filesavepath+newFileName1); } if (file2!=null) { String originalFileName2 = file2.getOriginalFilename(); newFileName2 = UUIDUtil.getuuid() + originalFileName2.substring(originalFileName2.lastIndexOf(".")); File newFile2 = new File(imgPath + File.separator + newFileName2); if( !newFile2.getParentFile().exists()) { // 如果目标文件所在的目录不存在,则创建父目录 newFile2.getParentFile().mkdirs(); } file2.transferTo(newFile2); ybsjAreaInfo.setAlogo("/"+filesavepath+newFileName2); } } catch (IllegalStateException | IOException e) { e.printStackTrace(); } int updateallareainfo = ybsjAreaInfoService.updateallareainfo(ybsjAreaInfo); String shaheScenicinfoid="{\"id\":\""+updateallareainfo+"\"}"; return shaheScenicinfoid; }
以上是 单个文件 接收的方法
如果用到一个Input 上传多个文件
后台用
@RequestParam("files") MultipartFile[] files
接收就好了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)