最近项目中需要用到文件上传,使用了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. iOS基础 - iOS程序启动原理

    一.UIApplicationMain 在main.m的main函数中执行了UIApplicationMain这个方法,这是ios程序的入口点 int UIApplicationMain(int ar ...

  2. SQL Server的数据加密简介

    防止开发人员获取到敏感数据(SQL Server的数据加密简介) 背景 有时候,我们还真的会碰到这样的需求:防止开发人员获取到敏感数据.也许你觉得很简单,把开发和运营分开不就可以了吗?是的,如果公司有 ...

  3. Linux环境进程间通信(二):信号(下)

    linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...

  4. Linux网络编程(三)

    Linux网络编程(三) wait()还是waitpid() Linux网络编程(二)存在客户端断开连接后,服务器端存在大量僵尸进程.这是由于服务器子进程终止后,发送SIGCHLD信号给父进程,而父进 ...

  5. tomcat的OutOfMemoryError内存溢出解决方法

    在tomcat安装路径bin目录下 打开catalina.bat在第一行加上 set JAVA_OPTS=-Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSi ...

  6. 5款最好用的开源Web快速开发工具

    1.Aptana Studio Aptana是一个用于HTML,CSS和JavaScript的网站开发工具.目前在社区里有成千上万的人在开发Aptana的插件. Apatana Studio官网:ht ...

  7. (Sql Server)数据的拆分和合并

    (Sql Server)数据的拆分和合并 背景: 今天遇到了数据合并和拆分的问题,尝试了几种写法.但大致可分为两类:一.原始写法.二.Sql Server 2005之后支持的写法.第一种写法复杂而且效 ...

  8. LINUX安装SVN+添加自动同步+远程下载最新代码

    LINUX安装SVN+添加自动同步+远程下载最新代码---------------------1. 新建一个用户:svnroot ,以下操作非特别说明皆为root用户操作--------------- ...

  9. grub 的安装与使用

    安装与使用grub 要开始探究 GRUB 的精妙之处,首先需要下载.编译和安装它.但不要害怕 -- 根本不会修改您的引导记录 -- 我们只是要编译和安装 GRUB,就像其它程序一样,在此过程中我们可以 ...

  10. 设计模式(二)-- 外观模式(Facade)

    设计模式(二) 外观模式(Facade) 为了解决子系统外部的客户端在使用子系统的时候,既能简单地使用这些子系统内部的模块功能,而又不用客户端去与子系统内部的多个模块交互的问题. 为子系统中的一组接口 ...