原文技术交流:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angularjs-ng-file-upload/

在做网站的过程中难免会遇到上传图片或者上传文件的功能,使用AngularJ实现的话可以用angularJs的ng-file-upload这个库。

  • 支持上传文件(目前为止我用过的是Excel上传,与上传图片的方法一样)
  • 支持单张图片上传
  • 支持多张图片上传
  • 支持拖拽图片上传

1.Install安装引用

  • 手册:可以从这里下载最新的js库
  • Bower安装:
    bower install ng-file-upload-shim --save(for non html5 suppport)
    bower install ng-file-upload --save
  • NuGet:PM> Install-Package angular-file-upload
  • NPM:npm install ng-file-upload
<script src="angular(.min).js"></script>
<script src="ng-file-upload-shim(.min).js"></script> <!-- for no html5 browsers support -->
<script src="ng-file-upload(.min).js"></script>

2.Usage用法

(1)Single image upload

image-upload.html:
//显示选择的图片
<img ngf-src="data.file || data.defaultImage" class="img-responsive" style="width:200px; height: 200px; max-width:500px;"/>
<div class="button" ngf-select="" ngf-pattern="'image/*'" ngf-multiple="false" ng-model="data.file">
选择文件
</div>
<button ng-click="upload()">upload</button> image-upload-ctrl.js:
$scope.data = {
file: null
};
$scope.upload = function () {
if (!$scope.data.file) {
return;
} var url = $scope.params.url; //params是model传的参数,图片上传接口的url
var data = angular.copy($scope.params.data || {}); // 接口需要的额外参数,比如指定所上传的图片属于哪个用户: { UserId: 78 }
data.file = $scope.data.file; Upload.upload({
url: url,
data: data
}).success(function (data) {
$scope.hide(data);
}).error(function () {
logger.log('error');
});
};

(2)Multiple images upload

<div class="button" ngf-select ngf-pattern="'image/*'" ng-model="files" ngf-multiple="true">选择多张图片</div>

//ngf-multiple控制是否可以上传多张图片

    // for multiple files:
$scope.upload = function (files) {
if (files && files.length) {
for (var i = ; i < files.length; i++) {
Upload.upload({..., data: {file: files[i]}, ...})...;
}
// or send them all together for HTML5 browsers:
Upload.upload({..., data: {file: files}, ...})...;
}
}

(3)Drop Single  image

image-upload.html:
<button class="btn btn-default" ngf-select="" ngf-pattern="'image/*'" ng-model="data.file"
ngf-multiple="false">
选择图片
</button>
<span>(支持拖拽单张图片上传)</span> <div ngf-drop ngf-pattern="'image/*'" ng-model="data.file" class="cropArea">
<img-crop image="data.file|| data.defaultImage" resule-image="data.file"></img-crop> <div class="col col-sm-4 text-left">
<img ngf-src="data.file|| data.defaultImage"
style="width:200px; height: 200px; max-width:500px;"/>
</div>
</div> <button class="btn btn-primary" ng-click="upload()">开始上传</button> style.css:
.cropArea {
background: #E4E4E4;
min-height: 230px;
height: auto;
margin: 15px;
margin-right: ;
}

(4)Drop and Select  Multiple images upload

image-upload.html:
<button class="btn btn-default" ngf-select="selectImage($files)" ngf-pattern="'image/*'"
ngf-multiple="true">
选择图片
</button>
<span>(支持多张图片拖拽上传)</span>
<div class="row cropArea" style="padding: 0 15px 15px 0" ngf-drop ngf-pattern="'image/*'" ng-model="files"
ngf-multiple="true">
<img-crop image="files || data.defaultImage" resule-image="files"></img-crop>
<div ngf-no-file-drop>该浏览器不支持拖拽上传。</div>
<div class="col col-xs-4 text-left" style="margin-top: 15px;" ng-repeat="image in mulImages">
<div ng-repeat="oneImage in image">
<img ngf-src="oneImage || data.defaultImage" class="img-responsive"
style="width:200px; height: 200px; max-width:500px;"/>
</div>
</div>
</div>
//<img-crop></img-crop>定义了图片可以拖拽的位置
//<img/>排列显示上传的多张图片 style.css:
.cropArea {
background: #E4E4E4;
min-height: 230px;
height: auto;
margin: 15px;
margin-right: ;
} image-upload-ctrl.js:
$scope.data = {
file: null,
defaultImage: commonSvc.defaultImage
}; $scope.mulImages = []; $scope.$watch('files', function () {
$scope.selectImage($scope.files);
});
//根据选择的图片来判断 是否为一下子选择了多张
//一下子选择多张的数据格式为一个数组中有多个对象,而一次只选择一张的数据格式为一个数组中有一个对象
$scope.selectImage = function (files) {
if (!files || !files.length) {
return;
}
if (files.length > ) {
angular.forEach(files, function (item) {
var image = [];
image.push(item);
$scope.mulImages.push(image);
})
} else {
$scope.mulImages.push(files);
}
}; $scope.upload = function () {
if (!$scope.mulImages.length) {
return;
}
var url = $scope.params.url;
var data = angular.copy($scope.params.data || {});
data.file = $scope.mulImages; Upload.upload({
url: url,
data: data
}).success(function (data) {
$scope.hide(data);
$rootScope.alert('success');
}).error(function () {
$rootScope.alert(‘error’);
}); };

以上内容是我做项目中需要的一些功能,也许不是该库全面的功能,若有其他需求可以查看GitHub官方文档:https://github.com/danialfarid/ng-file-upload

angularJs中上传图片/文件功能:ng-file-upload的更多相关文章

  1. [svc]linux中的文件描述符(file descriptor)和文件

    linux中的文件描述符(file descriptor)和文件 linux为了实现一切皆文件的设计哲学,不仅将数据抽象成了文件,也将一切操作和资源抽象成了文件,比如说硬件设备,socket,磁盘,进 ...

  2. 《Play for Java》学习笔记(六)文件上传file upload

    一. Play中标准方法 使用表单form和multipart/form-data的content-type类型. 1.Form @form(action = routes.Application.u ...

  3. 查找EBS中各种文件版本(Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER)

    Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER (文档 ID 85895 ...

  4. Java springboot 中上传图片文件的1Md的限制

    在配置文件中修改了 spring.servlet.multipart.max-file-size=3.40spring.servlet.multipart.max-request-size=3M 遇到 ...

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

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

  6. Warning: File upload error - unable to create a temporary file in Unknown on line 0

    upload_tmp_dir 临时文件夹问题 上传文件提示 Warning: File upload error - unable to create a temporary file in Unkn ...

  7. JQuery File Upload 插件 出现 “empty file upload result” 错误的解决方案。

    本例中采用的是 JQuery File Upload + ASP.NET 的方式, Google了大半天基本没有找到合理的解决方案,倒是在 NodeJS的一遍博客中找到了灵感:http://www.i ...

  8. jQuery file upload cropper的流程

    https://tkvw.github.io/jQuery-File-Upload/basic-plus-editor.html 最开始初始化jquery.ui.widget.js中的factory( ...

  9. ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案

    摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...

随机推荐

  1. iOS摄像头和相册-UIImagePickerController-浅析

    转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101cfd9.html 在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的 ...

  2. socket编写简单回显server

    socket在公司代码中应用比较广,比如接口调用的IPCRPC机制,经常看到这样的代码,但是一直也没有动手写过. 在某个比较大的进程中创建一个子进程,由于父子进程复制会浪费内存,可以将创建进程的命令通 ...

  3. 测试rest接口的两个工具使用详解(restclient+soapUI)

    http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/31/3110764.html http://www.oschina.net/news/618 ...

  4. Oracle Job相关

    Oracle JOB的建立,定时执行任务      begin            sys.dbms_job.submit(job => :job,                       ...

  5. jquery获取当前元素的坐标

    jquery获取当前元素的坐标 1,获取对象 var obj = $("#id号"); 或  var obj = $(this); 实例中我获取的对象是弹出窗口按钮,这样创建的新窗 ...

  6. (5)html表单

    本节解说: html的表单. form * <form> 标签用于为用户输入创建 HTML 表单. 表单能够包含 input 元素,比如文本字段.复选框.单选框.提交按钮等等. * < ...

  7. 功能强大支持64位操作系统的转Flash软件(doc转swf):Print2Flash

    Print2Flash是一个虚拟打印机类的文档转换软件,因此只要是可打印的文档,都可以轻松转换为Flash文件,即SWF动画,特别是用于转换PDF.Word.Excel.PowerPoint等文档为S ...

  8. 动态链接库 仅有.dll文件时候的使用方法

    在没有.h和.lib文件时,需要函数指针和WIN32 API函数 LoadLibrary, GetProcAddress 和FreeLibrary 只需要.dll文件即可(将.dll文件置入工程目录中 ...

  9. ASP.NET中IsPostBack属性研究

    通过页面的IsPostback属性,可以检查 .aspx 页是否为传递回服务器的页面:当加载页面并对控件的更改属性处理之前,用户可以在page_Load事件中检查该页面是否被传递回的页面. 一般是在p ...

  10. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...