欢迎来到代码驿站!

AngularJS

当前位置:首页 > 网页前端 > AngularJS

angularjs实现多张图片上传并预览功能

时间:2020-12-27 17:08:16|栏目:AngularJS|点击:

本文实例为大家分享了angularjs上传多张图片并预览的具体代码,供大家参考,具体内容如下

directive.js

/* 
 * 多图片上传及预览指令(需指定图片类名) 
 * 
 */ 
 
angular.module('routerModule').directive('fileModel', ['$parse', 'fileReader', function($parse, fileReader) { 
 return { 
 restrict:'A', 
  link:function(scope, element, attrs, ngModel) { 
  var model = $parse(attrs.fileModel); 
  var modelSetter = model.assign; 
  var imgviewID = attrs["imgViewId"]; 
  var imgView = angular.element(document.querySelector("."+imgviewID)); 
  element.bind('change', function(event) { 
  scope.$apply(function() { 
   modelSetter(scope, element[0].files[0]); 
  }); 
  //附件预览 
  scope.file = (event.srcElement || event.target).files[0]; 
  fileReader.readAsDataUrl(scope.file, scope).then(function(result) { 
   imgView.attr("src",result); 
  }); 
  }); 
 } 
 } 
}]); 
 
angular.module('routerModule').factory('fileReader', ["$q", "$log", function($q, $log) { 
 var onLoad = function(reader, deferred, scope) { 
 return function() { 
  scope.$apply(function() { 
  deferred.resolve(reader.result); 
  }); 
 } 
 } 
 
 var onError = function(reader, deferred, scope) { 
 return function() { 
  scope.$apply(function() { 
  deferred.reject(reader.result); 
  }); 
 }; 
 }; 
 
 var getReader = function(deferred, scope) { 
 var reader = new FileReader(); 
 reader.onload = onLoad(reader, deferred, scope); 
 reader.onerror = onError(reader, deferred, scope); 
 return reader; 
 }; 
 
 var readAsDataURL = function(file, scope) { 
 var deferred = $q.defer(); 
 var reader = getReader(deferred, scope); 
 reader.readAsDataURL(file); 
 return deferred.promise; 
 } 
 return { 
 readAsDataUrl: readAsDataURL 
 }; 
 } 
]); 

html关键代码

<section class="infogroup"> 
 <h4>法人证件</h4> 
 <div class="row relatedCard"> 
 <div class="col-md-4 txtcenter"> 
  <p>身份证正面</p> 
  <div><img ng-src="showImg/{{dealer.idCardFace}}" class="idCardFace" enlarge-pic/></div> 
  <span class="btn btn-file btn btn-xs btn-primary btn-editable picEdit"><i class="fa fa-edit"></i> 点击编辑 
   <input type="file" file-model="idCardFace" img-view-id="idCardFace" /> 
  </span> 
 </div> 
 <div class="col-md-4 txtcenter"> 
  <p>身份证反面</p> 
  <div><img ng-src="showImg/{{dealer.idCardBack}}" class="idCardBack" enlarge-pic/></div> 
  <span class="btn btn-file btn btn-xs btn-primary btn-editable picEdit"><i class="fa fa-edit"></i> 点击编辑 
   <input type="file" file-model="idCardBack" img-view-id="idCardBack" /> 
  </span> 
 </div>     
 </div> 
</section> 

controller.js中发送到后台的数据

$scope.postData = { 
 dealerId: $scope.dealer.dealerId, 
 companyName: $scope.dealer.companyName, 
 companySize: $scope.dealer.companySize, 
 idCardFace: angular.element(document.querySelector(".idCardFace"))[0].src, 
 idCardBack: angular.element(document.querySelector(".idCardBack"))[0].src 
}; 
$http.post('updateDealerCertificate',$scope.postData).success(function(data){ 
<!-- 成功执行代码 --> 
}); 

预览如下:

上一篇:Material(包括Material Icon)在Angular2中的使用详解

栏    目:AngularJS

下一篇:Angular实现的简单定时器功能示例

本文标题:angularjs实现多张图片上传并预览功能

本文地址:http://www.codeinn.net/misctech/37253.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有