小编给大家分享一下AngularJs如何实现上传前预览图片,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
demo.html:
<!doctype html> <html ng-app="myTestCtrl"> <head> <meta charset="UTF-8"> <title>demo</title> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script src="myCtrl.js"></script> <style type="text/css"> .inputBox{width: 160px; height: 28px; padding: 0 0 0 8px; box-sizing: border-box; background-color:#fff; margin-left: 5px; border: 1px solid #c4c4c4; color: #333; border-radius: 3px; -o-border-radius: 3px;-moz-border-radius: 3px;-webkit-border-radius: 3px;} .inputBox:focus{border: 1px solid #207fe9;} .btn-primary {color: #fff; background-color: #428bca; border-color: #357ebd;} .btn {display: inline-block; padding: 6px 12px; margin-bottom: 0; font-size: 14px; font-weight: 400;line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; -ms-touch-action: manipulation; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background-image: none; border: 1px solid transparent; border-radius: 4px;} .bg-bbbbbb{background-color: #bbb;} .fl{float:left;} .ml5{margin-left: 5px;} .ml10{margin-left: 10px;} .ml30{margin-left: 30px;} .mt10{margin-top: 10px;} .mt20{margin-top: 20px;} .f-cb:after:after{display:block;clear:both;visibility:hidden;height:0;overflow:hidden;content:".";} .f-cb{zoom:1;} .f-cb .topSearch{ float: left; margin-top: 10px; line-height: 30px; font-size: 12px; } </style> </head> <body id="myTestCtrl" ng-controller="myTestCtrl"> <div class="wrapper"> <div class="content"> <div class="f-cb" > <div class="topSearch"><span class="w70 tr dib fl">主视觉图:</span><input type="text" class="inputBox fl ml5" ng-model="fileName"><button class="btn btn-primary ml10" ng-class="{'bg-bbbbbb':imgDisabled}" img-upload></button></div> </div> <div class="f-cb mt10"><img ng-src="{{thumb.imgSrc}}" ng-show="thumb.imgSrc"/></div> </div> <div class="mt20 ml30"> <button class="btn btn-primary" ng-click="saveClick()" ng-class="{'bg-bbbbbb':submitDisabled}">提交</button> </div> </div> </body> </html> <span >myCtrl.js:</span> <pre name="code" class="javascript">//关键 js 部分 var myTestCtrl = angular.module('myTestCtrl', []); //定义“上传”指令,修改后也可用于上传其他类型的文件 myTestCtrl.directive("imgUpload",function(){ return{ //通过设置项来定义 restrict: 'AE', scope: false, template: '<div class="fl"><input type="button" id="storeBtn" value="选择文件"><input type="file" name="testReport" id="file" ng-disabled="imgDisabled" accept=".jpg,.png"></div>', //name:testReport 与接口字段相对应。 replace: true, link: function(scope, ele, attrs) { ele.bind('click', function() { $('#file').val(''); }); ele.bind('change', function() { scope.file = ele[0].children[1].files; if(scope.file[0].size > 52428800){ alert("图片大小不大于50M"); scope.file = null; return false; } scope.fileName = scope.file[0].name; var postfix = scope.fileName.substring(scope.fileName.lastIndexOf(".")+1).toLowerCase(); if(postfix !="jpg" && postfix !="png"){ alert("图片仅支持png、jpg类型的文件"); scope.fileName = ""; scope.file = null; scope.$apply(); return false; } scope.$apply(); scope.reader = new FileReader(); //创建一个FileReader接口 console.log(scope.reader); if (scope.file) { //获取图片(预览图片) scope.reader.readAsDataURL(scope.file[0]); //FileReader的方法,把图片转成base64 scope.reader.onload = function(ev) { scope.$apply(function(){ scope.thumb = { imgSrc : ev.target.result //接收base64,scope.thumb.imgSrc为图片。 }; }); }; }else{ alert('上传图片不能为空!'); } }); } }; }); myTestCtrl.controller("myTestCtrl", function($scope, $http) { //导入图片 $scope.saveClick = function () { //禁用按钮 $scope.imgDisabled = true; $scope.submitDisabled = true; var url = '';//接口路径 var fd = new FormData(); fd.append('testReport', $scope.file[0]);//参数 testReport=后台定义上传字段名称 ; $scope.file[0] 内容 $http.post(url, fd, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }).success(function (data) { if(data.code != 100) { alert(JSON.stringify('文件导入失败:'+files.files[0].name+',请重新上传正确的文件或格式')); }else{ alert(JSON.stringify('文件导入成功:'+files.files[0].name)); } //恢复按钮 $scope.imgDisabled = false; $scope.submitDisabled = false; }).error(function (data) { alert('服务器错误,文件导入失败!'); //恢复按钮 $scope.imgDisabled = false; $scope.submitDisabled = false; }); }; }); </pre> <br> <pre></pre> <p></p> <pre> </pre> <p></p> <pre> </pre>
以上是“AngularJs如何实现上传前预览图片”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。