前言
本文主要給大家介紹了關于Angular.js文件上傳控件ng-file-upload結合springMVC使用的相關內容,對于Angular.js文件上傳控件ng-file-upload不熟悉的朋友們可以先看看這篇文章(傳送門),下面話不多說,來看看詳細的使用介紹:
引入angular和ng-file-upload。
前端代碼
Upload.upload({ url: 'upload', fields: {'username': 'zouroto'}, // additional data to send file: file }).progress(function (evt) { var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name); }).success(function (data, status, headers, config) { console.log('file ' + config.file.name + 'uploaded. Response: ' + data); });
springMVC代碼:
@Controllerpublic class UiController { @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/upload") public void upload(@RequestParam("file") MultipartFile file, @RequestParam("username") String username ) throws IOException { byte[] bytes; if (!file.isEmpty()) { bytes = file.getBytes(); //store file in storage } System.out.println(String.format("receive %s from %s", file.getOriginalFilename(), username)); }}
application config
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5000000"/> </bean>
maven
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答