angularjs怎么实现多张图片上传并预览功能
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍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.class="lazy" data-srcElement || event.target).files[0];
fileReader.readAsDataUrl(scope.file, scope).then(function(result) {
imgView.attr("class="lazy" data-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">
<h5>法人证件</h5>
<div class="row relatedCard">
<div class="col-md-4 txtcenter">
<p>身份证正面</p>
<div><img ng-class="lazy" data-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-class="lazy" data-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].class="lazy" data-src,
idCardBack: angular.element(document.querySelector(".idCardBack"))[0].class="lazy" data-src
};
$http.post('updateDealerCertificate',$scope.postData).success(function(data){
<!-- 成功执行代码 -->
});
预览如下:
以上是“angularjs怎么实现多张图片上传并预览功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341