网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

AngularJs如何实现上传前预览图片

小编给大家分享一下AngularJs如何实现上传前预览图片,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名网站空间、营销软件、网站建设、甘孜州网站维护、网站推广。

demo.html:

 
 
 
  
 demo 
  
  
  
  
 .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; } 
  
 
 
 
  
   
   主视觉图:
    
    
          提交           myCtrl.js:  //关键 js 部分  var myTestCtrl = angular.module('myTestCtrl', []);  //定义“上传”指令,修改后也可用于上传其他类型的文件  myTestCtrl.directive("imgUpload",function(){   return{    //通过设置项来定义    restrict: 'AE',    scope: false,    template: '', //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;    });   };  });
 
 

 
 

 

以上是“AngularJs如何实现上传前预览图片”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


新闻名称:AngularJs如何实现上传前预览图片
本文来源:http://bjjierui.cn/article/psshdo.html