AngularJS上傳文件的示例代碼
使用AngularJS上傳文件
- 前臺(tái)是Angular頁(yè)面
- 后臺(tái)使用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 ,通過(guò)設(shè)置'Content-Type': undefined,這樣瀏覽器不僅幫我們把Content-Type 設(shè)置為 multipart/form-data,還填充上當(dāng)前的boundary,
- 如果手動(dòng)設(shè)置為:'Content-Type': multipart/form-data,后臺(tái)會(huì)拋出異常:the request was rejected because no multipart boundary was found
- boundary 是隨機(jī)生成的字符串,用來(lái)分隔文本的開始和結(jié)束
- 通過(guò)設(shè)置 transformRequest: angular.identity ,anjularjs transformRequest function 將序列化我們的formdata object,也可以不添加
后臺(tái)
@RequestMapping("/upload")
public void uploadFile(@RequestParam(value = "file" , required = true) MultipartFile file) {
//deal with file
}
注意
文件必須通過(guò)@RequestParam注解來(lái)獲取,且需指定value才能獲取到
這樣就完成了上傳文件
上傳文件的同時(shí)傳遞其他參數(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上,否則會(huì)直接被轉(zhuǎn)為字符串[Object,object]
后臺(tái)
@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對(duì)象解析為UserModel對(duì)象
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例
今天小編就為大家分享一篇angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Angular中innerHTML標(biāo)簽的樣式不起作用的原因解析
這篇文章主要介紹了Angular中innerHTML標(biāo)簽的樣式不起作用詳解 ,本文給出了解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
自學(xué)實(shí)現(xiàn)angularjs依賴注入
這篇文章主要為大家詳細(xì)介紹了angularjs依賴注入的自己成果,如何實(shí)現(xiàn)angularjs依賴注入,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
詳解Angular.js的$q.defer()服務(wù)異步處理
相信大家都知道jquery和angular都有defer服務(wù),這篇文章暫以angular為例談?wù)剛€(gè)人的理解,在文章的最后并附上jquery的阮一峰總結(jié)的defer。有需要的朋友們也可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11

