AngularJS上傳文件的示例代碼
使用AngularJS上傳文件
- 前臺是Angular頁面
- 后臺使用SpringBoot/SpirngMVC
上傳文件
html
<div> <input id="fileUpload" type="file" /> <button ng-click="uploadFile()">上傳</button> </div>
js
$scope.upload = function(){ var form = new FormData(); var file = document.getElementById("fileUpload").files[0]; form.append('file', file); $http({ method: 'POST', url: '/upload', data: form, headers: {'Content-Type': undefined}, transformRequest: angular.identity }).success(function (data) { console.log('upload success'); }).error(function (data) { console.log('upload fail'); }) }
注意:
- AngularJS默認(rèn)的'Content-Type'是application/json ,通過設(shè)置'Content-Type': undefined,這樣瀏覽器不僅幫我們把Content-Type 設(shè)置為 multipart/form-data,還填充上當(dāng)前的boundary,
- 如果手動設(shè)置為:'Content-Type': multipart/form-data,后臺會拋出異常:the request was rejected because no multipart boundary was found
- boundary 是隨機(jī)生成的字符串,用來分隔文本的開始和結(jié)束
- 通過設(shè)置 transformRequest: angular.identity ,anjularjs transformRequest function 將序列化我們的formdata object,也可以不添加
后臺
@RequestMapping("/upload") public void uploadFile(@RequestParam(value = "file" , required = true) MultipartFile file) { //deal with file }
注意
文件必須通過@RequestParam注解來獲取,且需指定value才能獲取到
這樣就完成了上傳文件
上傳文件的同時傳遞其他參數(shù)
html
<div> <input id="fileUpload" type="file" /> <button ng-click="ok()">上傳</button><br> <input ng-model="user.username" /> <input ng-model="user.password" /> </div>
js
$scope.ok = function () { var form = new FormData(); var file = document.getElementById("fileUpload").files[0]; var user =JSON.stringify($scope.user); form.append('file', file); form.append('user',user); $http({ method: 'POST', url: '/addUser', data: form, headers: {'Content-Type': undefined}, transformRequest: angular.identity }).success(function (data) { console.log('operation success'); }).error(function (data) { console.log('operation fail'); }) };
注意
需要將Object轉(zhuǎn)為String后在附加到form上,否則會直接被轉(zhuǎn)為字符串[Object,object]
后臺
@RequestMapping("/upload") public Map<String, Object> upload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "user", required = true) String user) { try (FileInputStream in = (FileInputStream) headImg.getInputStream(); FileOutputStream out = new FileOutputStream("filePathAndName")) { //將Json對象解析為UserModel對象 ObjectMapper objectMapper = new ObjectMapper(); UserModel userModel = objectMapper.readValue(user, UserModel.class); //保存文件到filePathAndName int hasRead = 0; byte[] bytes = new byte[1024]; while ((hasRead = in.read(bytes)) > 0) { out.write(bytes, 0, hasRead); } } catch (IOException e) { e.printStackTrace(); } }
注意
ObjectMapper為com.fasterxml.jackson.databind.ObjectMapper
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angularjs封裝bootstrap時間插件datetimepicker
這篇文章主要介紹了angularjs封裝bootstrap時間插件datetimepicker 的相關(guān)資料,需要的朋友可以參考下2016-06-06解決angularJS中input標(biāo)簽的ng-change事件無效問題
今天小編就為大家分享一篇解決angularJS中input標(biāo)簽的ng-change事件無效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09AngularJS ng-bind-html 指令詳解及實(shí)例代碼
本文主要是對AngularJS ng-bind-html 指令知識的詳細(xì)講解,并附代碼實(shí)例,有需要的小伙伴可以參考下2016-07-07詳談angularjs中路由頁面強(qiáng)制更新的問題
下面小編就為大家?guī)硪黄斦刟ngularjs中路由頁面強(qiáng)制更新的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Angular實(shí)現(xiàn)圖片裁剪工具ngImgCrop實(shí)踐
本篇文章主要介紹了Angular實(shí)現(xiàn)圖片裁剪工具ngImgCrop實(shí)踐,具有一定的參考價值,有興趣的可以了解一下2017-08-08AngularJS extend用法詳解及實(shí)例代碼
這篇文章主要介紹了AngularJS extend用法詳解的相關(guān)資料,并附實(shí)例代碼,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下2016-11-11