- <pre name="code" class="html">html代码</pre><input type="file" multiple="true" id="file" name="file"/><button class="u-button u-button-primary" onclick="uploadFileMethod1()" id="doUpload">确认附件</button>
- <pre></pre>
- <br>
- <p>js代码</p>
- <p>单个文件上传<br>
- </p>
- <p></p><pre name="code" class="plain"> function uploadFileMethodSingleFile()
- {
- var FileController = $ctx+"/fankuiController/saveFiles"; // 接收上传文件的后台地址
- // FormData 对象
- var fileName = document.getElementById("file").value;
- // alert(getPath(fileObj));
- var form = new FormData();
- var fileObj = document.getElementById("file").files[0]; // 获取文件对象
- form.append("file ",fileObj ); // 文件对象
- var xhr = new XMLHttpRequest();
- xhr.open("post", FileController, true);
- xhr.send(form);
- xhr.onload = function () {
- alert("上传完成!");
- };
- }</pre><br>
- 单个文件上传时,controller用MultipartFile 类型的参数接受文件<pre name="code" class="java">@RequestMapping(method = RequestMethod.POST,value = "saveFiles")
- public @ResponseBody Map<String,Object> saveFiles(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
- Map<String,Object> reg=new HashMap<String,Object>();
- //将内存中的数据写入磁盘
- System.out.println("开始进行附件上传");
- try {
- //String filePath = request.getSession().getServletContext().getRealPath("/");
- String filePath ="c:\\";
- MultipartFile file = fileArray[i];
- String originalFileName = file.getOriginalFilename();
- String newFileName = UUID.randomUUID()+originalFileName;
- String finalPath = filePath+newFileName;
- System.out.println(originalFileName);
- System.out.println(newFileName);
- System.out.println(finalPath);
- System.out.println("参数"+request.getParameter("json_filesNameArray"));
- System.out.println("file"+file.getContentType());
- File fileAttach = new File(finalPath);
- file.transferTo(fileAttach);
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- //上传成功后,将附件加入数据库附件表的blob字段中
- reg.put("result","success");
- return reg;
- }</pre><br>
- <p></p>
- <p>如果是多个文件上传 fomdata 中要使用formdata.append() 对多个文件进行遍历 然后进行上传</p>
- <p></p><pre name="code" class="javascript">function uploadFileMethod1()
- {
- var FileController = $ctx+"/fankuiController/saveFiles"; // 接收上传文件的后台地址
- // FormData 对象
- var fileName = document.getElementById("file").value;
- // alert(getPath(fileObj));
- var form = new FormData();
- form.append("json_filesNameArray", fileName); // 可以增加表单数据
- // var fileArray = new Array();
- // for(var i=0;i<fileArray.length;i++)
- // {
- // var fileObj = document.getElementById("file").files[i]; // 获取文件对象
- // fileArray.push(fileObj);
- // }
- var files = document.getElementById("file").files;
- for(var i=0; i< files.length; i++){
- alert(files[i].name);
- form.append("fileArray",files[i]); // 文件对象
- }
- // XMLHttpRequest 对象
- //var fileObj = document.getElementById("file").files[0];
- //form.append("fileArray", fileObj);
- var xhr = new XMLHttpRequest();
- xhr.open("post", FileController, true);
- xhr.send(form);
- xhr.onload = function () {
- alert("上传完成!");
- };
- }</pre>多文件上传时,controller端,进行接收时 使用 MultipartFile[] 类型参数 接受 数组类型的多个文件,然后遍历数组进行操作<p></p>
- <p></p><pre name="code" class="java">@RequestMapping(method = RequestMethod.POST,value = "saveFiles")
- public @ResponseBody Map<String,Object> saveFiles(@RequestParam(value = "fileArray") MultipartFile[] fileArray, HttpServletRequest request) {
- Map<String,Object> reg=new HashMap<String,Object>();
- //将内存中的数据写入磁盘
- System.out.println("开始进行附件上传");
- System.out.println(fileArray.length);
- try {
- for(int i=0;i<fileArray.length;i++)
- {
- //String filePath = request.getSession().getServletContext().getRealPath("/");
- String filePath ="c:\\";
- MultipartFile file = fileArray[i];
- String originalFileName = file.getOriginalFilename();
- String newFileName = UUID.randomUUID()+originalFileName;
- String finalPath = filePath+newFileName;
- System.out.println(originalFileName);
- System.out.println(newFileName);
- System.out.println(finalPath);
- System.out.println("参数"+request.getParameter("json_filesNameArray"));
- System.out.println("file"+file.getContentType());
- File fileAttach = new File(finalPath);
- file.transferTo(fileAttach);
- }
- } catch (Exception e1) {
- e1.printStackTrace();
- }</pre><br>
- <br>
- <p></p>
- <p><br>
- </p>
- <link rel="stylesheet" href="http://static.blog.csdn.net/public/res-min/markdown_views.css?v=2.0">
使用html+ajax+formdata+SpringMVC实现单个文件,和多个文件上传功能
最新推荐文章于 2024-06-16 11:02:37 发布