SpringMvc+Angularjs 實現(xiàn)多文件批量上傳
更新時間:2017年03月23日 11:03:10 作者:y0yO011
本文通過實例代碼給大家講解了SpringMvc+Angularjs 實現(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();
}
// 將文件拷貝到當前目錄下
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)多文件批量上,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的實例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
SpringBoot配置ShedLock分布式定時任務(wù)
ShedLock是一個在分布式環(huán)境中使用的定時任務(wù)框架,這篇文章主要介紹了SpringBoot配置ShedLock分布式定時任務(wù),需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
SpringBoot監(jiān)控模塊Actuator的用法詳解
Spring?Boot?Actuator?是?Spring?Boot?自帶的一個功能模塊,提供了一組已經(jīng)開箱即用的生產(chǎn)環(huán)境下常用的特性和服務(wù),比如應用程序的健康檢查、信息暴露、度量收集、日志記錄等,本文將給大家詳細SpringBoot監(jiān)控模塊Actuator的用法2023-06-06
手把手教你使用IDEA創(chuàng)建多模塊(maven)項目
這篇文章主要給大家介紹了關(guān)于如何使用IDEA創(chuàng)建多模塊(maven)項目的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07

