原文技术交流: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. Linux内核完全注释之编程语言和环境(二)

    c程序的编译和链接 使用gcc汇编器编译c语言程序时通常会经历四个阶段,即预处理阶段.编译阶段.汇编阶段.链接阶段,如下图. 例如: gcc -o hello hello.c 生成可执行文件hello ...

  2. yii2.0 gii

    1.添加模型 ./yii-dev gii/model --tableName=tableName --generateLabelsFromComments --ns='app\models\base' ...

  3. easyui中tree型控件不正常显示的处理方法

    我在使用easyui中的tree控件时,出现不正常显示的现象,比如li中不能使用自定义的图标.父级展开或关闭时,其子级仍然显现并出现重叠等.找了很多资料,都没解决这个问题,后来逐个对照官方的源码,才找 ...

  4. mvc4帮助类

    @App @AppState @Ajax @Cache @Context @Culture @Html @IsPost @Layout @Model @Output @OutputStack @Pag ...

  5. Linux 下安装android

    主要参考了这篇文章 http://segmentfault.com/a/1190000003069062#articleHeader2 这里提到了“unable to run mksdcard sdk ...

  6. position:sticky用法

    用户的屏幕越来越大,而页面太宽的话会不宜阅读,所以绝大部分网站的主体宽度和之前相比没有太大的变化,于是浏览器中就有越来越多的空白区域,所以你可能注意到很多网站开始在滚动的时候让一部分内容保持可见,比如 ...

  7. Apache、Tomcat、JBoss、Weblogic四个常用的JavaEE服务器

    Apache全球应用最广泛的http服务器,免费,出自apache基金组织 Tomcat应用也算非常广泛的web 服务器,支持部分j2ee,免费,出自 apache基金组织 JBoss开源的应用服务器 ...

  8. C++学习笔记之输入、输出和文件

    一.流的概念 数据从内存的一个地址移动到另一个地址称为数据流动——流操作 流操作是通过缓冲区(buffer)机制实现的. 缓冲区:内存的一块区域——用作文件与内存交换数据. 数据从文件中读出:文件 → ...

  9. ubuntu完美卸载JDK

    要删除 OpenJDK (如果已安装的话).首先,检查是安装的哪个 OpenJDK包. # dpkg --list | grep -i jdk 移除 openjdk包: # apt-get purge ...

  10. 专注网格剖分 - TetGen

    提要 记得大三那一年有一门课叫做高等有限元,最后的作业就是网格剖分算法的实现,我和同学一起花了些时间做了一个Qt程序,他写算法,我写界面,最后成绩竟然出奇的拿了90多... 今天要介绍的这款软件Tet ...