多文件上传之springMVC
1、前端
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <c:set var="ctx" value="${pageContext.request.contextPath}"/> <script type="text/javascript" src="${ctx}/EasyUI/jquery.min.js"></script> </head> <body> <%--多文件上传--%> <form id="batchUploadRiskFileForm" action="" method="post" enctype="multipart/form-data"> 选择文件:<input id="uploadFiles" type="file" multiple="multiple"> <span class="easyui-linkbutton" onclick="batchUpdateRiskRule()">提交</span> </form> </body> </html> <script> //批量上传 function batchUpdateRiskRule() { var formData = new FormData(); var fileInput = document.getElementById('uploadFiles'); var files = fileInput.files; // alert(files.length); for (var i = 0; i < files.length; i++) { if (files[i] != null) { formData.append("file", files[i]); } } $.ajax({ url: '${ctx}/resources/uploadFiles', type: "post", fileElementId: "file", // 上传文件的id、name属性名 dataType : 'json', // 接受数据格式 data: formData, mimeType: 'multipart/form-data', async: false, contentType: false, processData: false, // secureuri: true, //一般设置为false success: function (msg) {
alert(msg); }, error: function (msg) { alert("error:"+msg); } }); } </script>
2、后端
//文件上传处理 @RequestMapping(value = "/resources/uploadFiles", method = RequestMethod.POST)
public List<HashMap<String, String>> uploadBannerFiles(SiteResources siteResource, @RequestParam("file") MultipartFile[] files, HttpSession session) throws Exception {
//处理
}