最近项目中需要用到文件上传,使用了angular-file-upload插件完成

首先来介绍下这个插件的一些属性(参考官方文档)

FileUploader

属性

  • url {String}: 上传文件的服务器路径
  • alias {String}:  包含文件的名称,默认是file
  • queue {Array}: 上传队列
  • progress {Number}: 上传队列的进度,只读
  • headers {Object}: 上传的头文件信息, 浏览器需支持HTML5
  • formData {Array}: 与文件一起发送的表单数据
  • filters {Array}: 在文件加入上传队列之前应用过滤器.,如果过滤器返回true则文件加入队列中
  • autoUpload {Boolean}: 文件加入队列之后自动上传,默认是false
  • method {String}: 请求方式,默认是POST,浏览器需支持HTML5
  • removeAfterUpload {Boolean}: 文件上传成功之后从队列移除,默认是false
  • isHTML5 {Boolean}: 如果浏览器支持HTML5上传则返回true,只读
  • isUploading {Boolean}: 文件正在上传中返回true,只读
  • queueLimit {Number} : 最大上传文件数量(预定义)
  • withCredentials {Boolean} : 使用CORS,默认是false, 浏览器需支持HTML5

方法

  • addToQueue function(files[, options[, filters]]) {: Add items to the queue, where files is a {FileList|File|HTMLInputElement}options is an {Object} andfilters is a {String}.  添加项到上传队列中,files 是 {FileList|File|HTMLInputElement}, options 是 {Object} 以及 filters 是 {String}
  • removeFromQueue function(value) {: Remove an item from the queue, wherevalue is {FileItem} or index of item.  从上传队列移除项,value 可以是 {FileItem} 或者项的序号
  • clearQueue function() {: Removes all elements from the queue.  移除上传队列所有的元素
  • uploadItem function(value) {: Uploads an item, where value is {FileItem} or index of item.  上传项, value 可以是 {FileItem} 或者项的序号
  • cancelItem function(value) {: Cancels uploading of item, where value is{FileItem} or index of item.  取消上传的项
  • uploadAll function() {: Upload all pending items on the queue.  将上传队列中所有的项进行上传
  • cancelAll function() {: Cancels all current uploads.  取消所有当前上传
  • destroy function() {: Destroys a uploader.
  • isFile function(value) {return {Boolean};}: Returns true if value is {File}.
  • isFileLikeObject function(value) {return {Boolean};}: Returns true if value is{FileLikeObject}.
  • getIndexOfItem function({FileItem}) {return {Number};}: Returns the index of the{FileItem} queue element.  返回项在上传队列中的序号
  • getReadyItems function() {return {Array.<FileItems>};}: Return items are ready to upload.  返回准备上传的项
  • getNotUploadedItems function() {return {Array.<FileItems>};}: Return an array of all pending items on the queue  返回上传队列中未上传的项

回调函数

  • onAfterAddingFile function(item) {: 添加文件到上传队列后
  • onWhenAddingFileFailed function(item, filter, options) {: 添加文件到上传队列失败后
  • onAfterAddingAll function(addedItems) {: 添加所选的所有文件到上传队列后
  • onBeforeUploadItem function(item) {: 文件上传之前
  • onProgressItem function(item, progress) {: 文件上传中
  • onSuccessItem function(item, response, status, headers) {: 文件上传成功后
  • onErrorItem function(item, response, status, headers) {: 文件上传失败后
  • onCancelItem function(item, response, status, headers) { - 文件上传取消后
  • onCompleteItem function(item, response, status, headers) {: 文件上传完成后
  • onProgressAll function(progress) {: 上传队列的所有文件上传中
  • onCompleteAll function() {: 上传队列的所有文件上传完成后

使用

当然首先需要加入插件的js

bower

bower install angular-file-upload

 在页面导入js

<script src="bower_components/angular-file-upload/dist/angular-file-upload.min.js"></script>
加入angularFileUpload
var myapp = angular.module('add',['angularFileUpload'])

html

我这里是上传的图片所以代码如下:

 <div ng-controller="addProduct">
<div>
<lable>产品名称</lable>
<input type="text" ng-model="productInfo.name">
</div>
<div>
<lable>产品型号</lable>
<input type="text" ng-model="productInfo.type">
</div>
<div>
<lable>产品图片</lable>
<input type="file" name="photo" nv-file-select="" uploader="uploader" accept="image/*" ngf-max-size="2MB" ngf-model-invalid="errorFile" /></div>
<div><button class="btn btn-info" ng-click="addProduct()"></div>
</div>

这个是最简单的使用主要是uploader这个属性,其他的accept、ngf-max-size、ngf-model-invalid都是一些限制图片的属性

Js

 myapp.controller('addProduct',['$scope','$http','FileUploader',function($scope,$http,FileUploader){

 //在外围定义一个数组,赋值给formData,通过改变此数组,实现数据的改变
var productInfo=[];
var uploader = $scope.uploader = new FileUploader({
url: 'add',
formData:productInfo
});
uploader.onSuccessItem = function(fileItem, response, status, headers) {
alert(response);
};
$scope.addProduct = function() {
uploader.uploadAll(); }
}])

java

     @RequestMapping(value="add",method = RequestMethod.POST)
public ResponseEntity<Object> addProduct(@RequestParam("file") MultipartFile uploadFiles,ProductVo productVo){ String fileName=uploadFile.getOriginalFilename();
String prefix="."+fileName.substring(fileName.lastIndexOf(".")+1);
File dst=null;
try {
String root = System.getProperty("catalina.base"); //获取tomcat根路径
File uploadDir = new File(root, "webapps/upload"); //创建一个指向tomcat/webapps/upload目录的对象
if (!uploadDir.exists()) {
uploadDir.mkdir(); //如果不存在则创建upload目录
}
dst = new File(uploadDir,
UUID.randomUUID().toString()+prefix); //创建一个指向upload目录下的文件对象,文件名随机生成
uploadFile.transferTo(dst); //创建文件并将上传文件复制过去
} catch (Exception e) {
e.printStackTrace();
}
//然后把路径set到productVo中 完成添加 "/upload/"+dst.getName(); }

如此就完成了。

主要问题

在Js中给formData赋值 因为formData的new生成的所以 就是固定不变的,如果直接写formData:[$scope.prodctInfo],就会导致formData没有值,后台就获取不到其他数据了。

如果失败的话可以调用onBeforeUploadItem function(item)这个方法,给formData重新赋值,达到修改的目的。如下:

uploader.onBeforeUploadItem function(item){
formData:“最终需要传递的值”
}

angular-file-upload+springMVC的使用的更多相关文章

  1. angularjs file upload插件使用总结

    之前由于项目需要,决定使用angularjs做前端开发,在前两个项目中都有文件上传的功能,因为是刚接触angularjs,所以对一些模块和模块间的依赖不是很了解.都是由其他大神搭好框架,我只做些简单的 ...

  2. html5 file upload and form data by ajax

    html5 file upload and form data by ajax 最近接了一个小活,在短时间内实现一个活动报名页面,其中遇到了文件上传. 我预期的效果是一次ajax post请求,然后在 ...

  3. Angular2 File Upload

    Angular2 File Upload Install Install the components npm install ng2-file-upload --save github: https ...

  4. Spring MVC-表单(Form)标签-文件上传(File Upload)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_upload.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显 ...

  5. [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor

    The upload class will be used in the service layer. Notice it has a constructor for file attribute, ...

  6. jquery file upload示例

    原文链接:http://blog.csdn.net/qq_37936542/article/details/79258158 jquery file upload是一款实用的上传文件插件,项目中刚好用 ...

  7. jQuery File Upload 单页面多实例的实现

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

  8. jQuery File Upload done函数没有返回

    最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...

  9. kindeditor多图片上传找不到action原来是private File upload成员变量惹得祸

    kindeditor多图片上传找不到action原来是private File upload成员变量惹得祸

  10. 【转发】Html5 File Upload with Progress

    Html5 File Upload with Progress               Posted by Shiv Kumar on 25th September, 2010Senior Sof ...

随机推荐

  1. c# in deep 之对Linq表达式范围变量限制问题的一些解决办法

    linq表达式的标准形式为from...where...select,其中from后面跟的就是范围变量.linq中范围变量需要是泛型的集合,假如我们想对ArrayList或Object[]进行处理,l ...

  2. 基于Stm32的MP3播放器设计与实现

    原创博文,转载请注明出处 这是我高级电子技术试验课做的作业,拿来共享一下.项目在安福莱例程基础之上进行的功能完善,里面的部分内容可参考安福莱mp3例程.当然用的板子也是安福莱的板子,因为算起来总共做了 ...

  3. 网络tcp/ip资料

    1. Linux TCP/IP 协议栈分析,这是chinaunix.net论坛里的N人写的电子书,可以在这里下载PDF版本.http://blog.chinaunix.net/u2/85263/sho ...

  4. FormsAuthentication知多少

    前述:对于FormsAuthentication相信大家都烂熟于胸了,这里只是做一下小结. 一.先看一下使用FormsAuthentication做登录认证的用法 用法一: FormsAuthenti ...

  5. MVC AuthorizeAttribute 动态授权

    开发中经常会遇到权限功能的设计,而在MVC 下我们便可以使用重写 AuthorizeAttribute 类来实现自定义的权限认证 首先我们的了解 AuthorizeAttribute 下面3个主要的方 ...

  6. .NET 微信开放平台接口(接收短信、发送短信)

    .NET 微信开放平台接口(接收短信.发送短信) 前两天做个项目用到了微信api功能.项目完成后经过整理封装如下微信操作类. 以下功能的实现需要开发者已有微信的公众平台账号,并且开发模式已开启.接口配 ...

  7. 【转载】Stack Overflow: The Architecture - 2016 Edition

    转载:http://www.infoq.com/cn/news/2016/03/Stack-Overflow-architecture-insi?utm_source=tuicool&utm_ ...

  8. 安装Theano

    参考文档 http://deeplearning.net/software/theano/install_centos6.html#install-centos6 安装依赖库 sudo yum ins ...

  9. JQUERY UI DOWNLOAD

    JQUERY UI DOWNLOAD jDownload是jQuery的一个下载插件,用户可以在下载文件之前知道文件的详细信息,在提高用户体验度方面起到了很大的作用. 鉴于官网的Demo是通过PHP文 ...

  10. PHP之算法

    PHP之算法偶遇隨感 要求如下:    第1种: A,B,C    期望能够得到的组合是: AB,AC,BC        第2种: A,B,C,D(可通过参数控制结果长度,如长度为2或3)    期 ...