SpringMvc+Angularjs 實現(xiàn)多文件批量上傳
更新時間:2017年03月23日 11:03:10 作者:y0yO011
本文通過實例代碼給大家講解了SpringMvc+Angularjs 實現(xiàn)多文件批量上傳功能,非常不錯,具有參考借鑒價值,需要的朋友一起學(xué)習(xí)吧
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 實現(xiàn)多文件批量上,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:
- AngularJS實現(xiàn)圖片上傳和預(yù)覽功能的方法分析
- Angularjs實現(xiàn)上傳圖片預(yù)覽功能
- angularjs點擊圖片放大實現(xiàn)上傳圖片預(yù)覽
- angularjs實現(xiàn)多張圖片上傳并預(yù)覽功能
- AngularJs上傳前預(yù)覽圖片的實例代碼
- 學(xué)習(xí)使用AngularJS文件上傳控件
- angularjs客戶端實現(xiàn)壓縮圖片文件并上傳實例
- AngularJS 文件上傳控件 ng-file-upload詳解
- 通過AngularJS實現(xiàn)圖片上傳及縮略圖展示示例
- Angularjs實現(xiàn)多圖片上傳預(yù)覽功能
相關(guān)文章
SpringBoot整合Mybatis與druid實現(xiàn)流程詳解
這篇文章主要介紹了springboot整合mybatis plus與druid詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的下伙伴可以參考一下2022-10-10
java serialVersionUID解決序列化類版本不一致問題面試精講
這篇文章主要為大家介紹了serialVersionUID解決序列化類版本不一致問題的面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
使用java基礎(chǔ)類實現(xiàn)zip壓縮和zip解壓工具類分享
使用java基礎(chǔ)類寫的一個簡單的zip壓縮解壓工具類,實現(xiàn)了指定目錄壓縮到和該目錄同名的zip文件和將zip文件解壓到指定的目錄的功能2014-03-03
基于Rest的API解決方案(jersey與swagger集成)
下面小編就為大家?guī)硪黄赗est的API解決方案(jersey與swagger集成)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

