总结一下,在写下这些文字之前心里很不爽,一个小问题倒腾了这么久...

 JS 端:

  

// 指令
app.directive('fileModel', ['$parse', function($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign; element.bind('change', function() {
scope.$apply(function() {
modelSetter(scope, element[0].files[0]);
})
})
}
}
}])
// Service
app.service('fileUpload', ['$http', function($http) {
this.uploadFile = function(file, url) {
var fd = new FormData();
fd.append('name', file.name);
fd.append('file', file); $http.post(url, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(data, status) { })
.error(function(data, status) { })
}
}])
// Controller
app.controller('FileUploadModalCtrl', ['$scope', '$uibModalInstance', 'fileUpload', function($scope, $uibModalInstance, fileUpload) { $scope.ok = function() {
var file = $scope.modelFile;
console.log('file is ' + file); var uploadUrl = './api/file/upload';
fileUpload.uploadFile(file, uploadUrl); $uibModalInstance.close($scope.items);
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
}
}]).controller('UploadCtrl', function($scope, $uibModalInstance, modelSrc, imgSrc) {
$scope.model_src = modelSrc;
$scope.img_src = imgSrc; });
<!-- HTML -->
<div class="form-group">
<label for="inputFile">File Input</label>
<input type="file" file-model="modelFile">
</div>

Spring 端


 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("file")
public class FileUploadService { @RequestMapping(method = RequestMethod.POST, value = "/upload")
public String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) { // todo what you want
}
}
<!-- 这个千万别忘了,在spring配置文件里声明 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

注释就不加了,下班了 :)

AngularJS 通过 Spring Restful 上传文件的更多相关文章

  1. Spring MVC上传文件

    Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...

  2. Spring MVC 上传文件

    Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data"  input的typ ...

  3. springboot(十七):使用Spring Boot上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...

  4. (转)Spring Boot(十七):使用 Spring Boot 上传文件

    http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...

  5. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  6. 使用Spring Boot上传文件

    原文:http://www.cnblogs.com/ityouknow/p/8298344.html 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spri ...

  7. Spring MVC上传文件原理和resolveLazily说明

    问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...

  8. Spring Boot(十七):使用 Spring Boot 上传文件

      上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 ...

  9. Spring Boot上传文件(带进度条)

    Spring Boot 上传文件(带进度条)# 配置文件 spring: freemarker: template-loader-path: classpath:/static/ ##Spring B ...

随机推荐

  1. 在Yii2中使用Pjax导致Yii2内联脚本载入失败的问题

    当我用defunkt/jquery-pjax载入Yii2的ActiveForm时发生一个错误,正常情况下是 ActiveForm的两个js应该先载入,而实际情况是 typeError:JQuery(. ...

  2. 使用pcs api往免费的百度网盘上传下载文件

    百度个人云盘空间大,完全免费,而且提供了pcs api供调用操作文件,在平时的项目里往里面保存一些文件是很实用的. 环境准备: 开通读写网盘的权限及获取access_token:http://blog ...

  3. android 集成百度地图

    一.下载百度地图为我们提供的所有DEMO. 在这里边我选的是一键下载. 二.下载后有两个项目一个是用于eclipse.另一个是android studio.我选的是android studio. 我用 ...

  4. Codeforces 519E A and B and Lecture Rooms

    http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...

  5. 将某个Qt4项目升级到Qt5遇到的问题(13条方法)

    本文转载自http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87 一.将某个QT4项目改成QT5遇到的问题 该Qt4项目以前是使用Qt4.7 ...

  6. 关于iostream的效率问题

    前言    经常有人说iostream的速度慢,IO流比stdio的慢多了.但是有人测试过的,iostream的速度是超过stdio的. 测试结果 /* C */#include <stdio. ...

  7. BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 414  Solved: ...

  8. 特征提取(Detect)、特征描述(Descriptor)、特征匹配(Match)的通俗解释

    特征匹配(Feature Match)是计算机视觉中很多应用的基础,比如说图像配准,摄像机跟踪,三维重建,物体识别,人脸识别,所以花一些时间去深入理解这个概念是不为过的.本文希望通过一种通俗易懂的方式 ...

  9. 批量更新sql |批量update sql

    图所示现需要批量更新table2表内字段Pwd更新userName对IP地址username与Ip对应关系table1所示 update table2 set pwd=table1.ip from t ...

  10. hdu5035:概率论推公式

    题目大意: 你要去邮局发一个包裹,有n个窗口,每个都有人,每一个窗口完成一次服务的时间 ti 的分布符合几何分布:ki*e^(-ki*t) 每个窗口当前服务已经进行了ci时间 你会去第一个完成当前服务 ...