AngularJS 通过 Spring Restful 上传文件
总结一下,在写下这些文字之前心里很不爽,一个小问题倒腾了这么久...
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 上传文件的更多相关文章
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- Spring MVC 上传文件
Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data" input的typ ...
- springboot(十七):使用Spring Boot上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...
- (转)Spring Boot(十七):使用 Spring Boot 上传文件
http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...
- Spring Boot(十七):使用Spring Boot上传文件
Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...
- 使用Spring Boot上传文件
原文:http://www.cnblogs.com/ityouknow/p/8298344.html 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spri ...
- Spring MVC上传文件原理和resolveLazily说明
问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...
- Spring Boot(十七):使用 Spring Boot 上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 ...
- Spring Boot上传文件(带进度条)
Spring Boot 上传文件(带进度条)# 配置文件 spring: freemarker: template-loader-path: classpath:/static/ ##Spring B ...
随机推荐
- UCOS 信号量
uCOS-II信号量OSSemCreate(0)和OSSemCreate(1)详解 (2014-04-22 18:04:18) 转载▼ 标签: it 分类: 操作系统 在ucos-II中,为了实现任务 ...
- C语言 创建一个 txt 文件 bin输入字符 保存文件在工作文件夹里
int main(void) { char s[70]; FILE *fp; fp=fopen("123.txt","r"); if((fp=fopen(&qu ...
- 64位调试器花费的时间比预期的要长(A 64-bit debugging operation is taking longer than expected)
在stackoverflow上找到解决方案的: http://stackoverflow.com/questions/21329899/vs2013-professional-local-64-bit ...
- Android 得到当前已连接的wifi的信号强度
1.得到当前已连接的wifi信息 WifiManager wifi_service = (WifiManager)getSystemService(WIFI_SERVICE); WifiInfo wi ...
- Windows Azure 存储管理器 (2014)
Windows Azure存储用户经常希望能够在"管理器"中查看他们的数据,管理器指的是一款可用于显示存储帐户数据的工具.我们之前提供了我们所知的存储管理器列表.在本文中,我 ...
- 除了修改WEBCONFIG会导致WEB服务重启外,还有其他的什么操作会导致重启?
1.修改WEBCONFIG文件 2.BIN文件夹下,添加.删除.覆盖文件 3.IIS应用程序池回收 参考文章:http://blog.csdn.net/hb_gx/archive/2007/05/ ...
- Annotation(一)——注解开发介绍
<p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...
- poj 2046 Gap(bfs+hash)
Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...
- PHP设计模式笔记九:装饰器模式 -- Rango韩老师 http://www.imooc.com/learn/236
装饰器模式(Decorator) 概述 1.装饰器模式可以动态地添加修改类的功能 2.一个类提供了一项功能,如果要在修改并添加额外的功能,传统的编程模式,需要写一个子类继承它,并重新实现类的方法 3. ...
- 响应式布局:Flexbox应用总结
距离上篇文章<布局神器:Flexbox>的发表已有一周时间,转眼这周又到了周五(O(∩_∩)O~~): 习惯性在周五对自己的一周工作进行下总结,记录下这周值得被纪念的工作事件,无论是好的, ...