SpringMvc+Angularjs 實(shí)現(xiàn)多文件批量上傳
SpringMvc代碼
jar包
commons-fileupload
commons-io
spring-mvc.xml配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> </bean>
Controller
@RequestMapping(value = "api/v1/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map upload (@RequestParam(value = "files") MultipartFile [] files, @RequestParam(value = "id") String id, HttpServletRequest request, HttpServletResponse response) { Map res = new HashMap(); try { log.info("upload>>>>>id:{}", id); if (files!=null) { for (MultipartFile file:files) { log.info("filename:{}", file.getOriginalFilename()); } } } catch (Exception e) { log.error("upload>>>>異常:{}", e.toString()); } log.info("upload>>>>返回結(jié)果:{}", res); return res; }
保存到本地
// copy File public boolean copyFile (MultipartFile tempFile, String filePath) { Boolean res = false; try { File file = new File(filePath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // 將文件拷貝到當(dāng)前目錄下 tempFile.transferTo(file); res = true; } catch (Exception e) { log.info("copyFile>>>>異常:{}", e.toString()); } return res; }
AngularJs代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="uploadCtrl"> <p><input type="file" multiple="multiple" name="files"></p> <p><input type="text" name="id" ng-model="id"></p> <p><input type="button" value="提交" ng-click="submit()"></p> </div> <script> var app = angular.module('myApp', []); app.controller('uploadCtrl', ["$scope", "$http", function($scope, $http) { $scope.submit = function () { var fd = new FormData(); var files = document.querySelector('input[name="files"]').files; for (var i=0; i<files.length; i++) { fd.append("files", files[i]); } fd.append("id", $scope.id); $http({ method:'POST', url : '/Project/api/v1/upload', data: fd, headers: {'Content-Type':undefined}, transformRequest: angular.identity }).success(function (response) { console.log(response.data); }).error(function () { }); } }]); </script> </body> </html>
Form表單提交
<form action="/Project/api/v1/upload" method="POST" enctype="multipart/form-data"> <p><input type="text" name="id" /></p> <p><input type="file" multiple="multiple" id="files" name="files" /></p> <p><input type="submit" value="Submit" /></p> </form>
以上所述是小編給大家介紹的SpringMvc+Angularjs 實(shí)現(xiàn)多文件批量上,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- AngularJS實(shí)現(xiàn)圖片上傳和預(yù)覽功能的方法分析
- Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能
- angularjs點(diǎn)擊圖片放大實(shí)現(xiàn)上傳圖片預(yù)覽
- angularjs實(shí)現(xiàn)多張圖片上傳并預(yù)覽功能
- AngularJs上傳前預(yù)覽圖片的實(shí)例代碼
- 學(xué)習(xí)使用AngularJS文件上傳控件
- angularjs客戶(hù)端實(shí)現(xiàn)壓縮圖片文件并上傳實(shí)例
- AngularJS 文件上傳控件 ng-file-upload詳解
- 通過(guò)AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例
- Angularjs實(shí)現(xiàn)多圖片上傳預(yù)覽功能
相關(guān)文章
SpringBoot整合Mybatis與druid實(shí)現(xiàn)流程詳解
這篇文章主要介紹了springboot整合mybatis plus與druid詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的下伙伴可以參考一下2022-10-10java serialVersionUID解決序列化類(lèi)版本不一致問(wèn)題面試精講
這篇文章主要為大家介紹了serialVersionUID解決序列化類(lèi)版本不一致問(wèn)題的面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10使用java基礎(chǔ)類(lèi)實(shí)現(xiàn)zip壓縮和zip解壓工具類(lèi)分享
使用java基礎(chǔ)類(lèi)寫(xiě)的一個(gè)簡(jiǎn)單的zip壓縮解壓工具類(lèi),實(shí)現(xiàn)了指定目錄壓縮到和該目錄同名的zip文件和將zip文件解壓到指定的目錄的功能2014-03-03Idea 搭建Spring源碼環(huán)境的超詳細(xì)教程
這篇文章主要介紹了Idea 搭建Spring源碼環(huán)境,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10mybatis分頁(yè)絕對(duì)路徑寫(xiě)法過(guò)程詳解
這篇文章主要介紹了mybatis分頁(yè)絕對(duì)路徑寫(xiě)法過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12基于Rest的API解決方案(jersey與swagger集成)
下面小編就為大家?guī)?lái)一篇基于Rest的API解決方案(jersey與swagger集成)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Java添加事件監(jiān)聽(tīng)的四種方法代碼實(shí)例
這篇文章主要介紹了Java添加事件監(jiān)聽(tīng)的四種方法代碼實(shí)例,本文直接給出代碼示例,并用注釋說(shuō)明,需要的朋友可以參考下2014-09-09