AngularJs上传前预览图片的实例代码


Posted in Javascript onJanuary 20, 2017

在工作中,使用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" style="height: 40px;"> 
   <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}" style="width:60px; margin-top:-3px; height:18px; position: relative;" img-upload></button></div> 
  </div> 
  <div class="f-cb mt10"><img ng-src="{{thumb.imgSrc}}" style="width:200px; height: 200px;" 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 style="font-size:14px;">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" style="padding:0; position: absolute; top: 0; left: 0; background: none; border: none;color: #fff; width:84px; height: 30px; line-height: 30px;" value="选择文件"><input type="file" name="testReport" id="file" ng-disabled="imgDisabled" style="position: absolute; top: 0; left: 0; opacity: 0;height: 30px;" 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的知识大家可以参考下小编给大家整理的专题,angularjs学习笔记,一起看看吧!

以上所述是小编给大家介绍的AngularJs上传前预览图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
chrome浏览器不支持onmouseleave事件的解决技巧
May 31 Javascript
模拟用户点击弹出新页面不会被浏览器拦截
Apr 08 Javascript
理解 JavaScript Scoping &amp; Hoisting(二)
Nov 18 Javascript
基于jQuery实现交互体验社会化分享代码附源码下载
Jan 04 Javascript
jQuery Mobile漏洞会有跨站脚本攻击风险
Feb 12 Javascript
无循环 JavaScript(map、reduce、filter和find)
Apr 08 Javascript
Vue组件之极简的地址选择器的实现
May 31 Javascript
JQuery判断radio单选框是否选中并获取值的方法
Jan 17 jQuery
原生JS封装拖动验证滑块的实现代码示例
Jun 01 Javascript
基于element-ui对话框el-dialog初始化的校验问题解决
Sep 11 Javascript
Flexible.js可伸缩布局实现方法详解
Nov 13 Javascript
JavaScript使用setTimeout实现倒计时效果
Feb 19 Javascript
详解JavaScript中this的指向问题
Jan 20 #Javascript
详解微信小程序入门五: wxml文件引用、模版、生命周期
Jan 20 #Javascript
浅谈JavaScript异步编程
Jan 20 #Javascript
JavaScript实现事件的中断传播和行为阻止方法示例
Jan 20 #Javascript
小程序开发实战:实现九宫格界面的导航的代码实现
Jan 19 #Javascript
BootStrap组件之进度条的基本用法
Jan 19 #Javascript
微信小程序 页面跳转和数据传递实例详解
Jan 19 #Javascript
You might like
typecho插件编写教程(四):插件挂载
2015/05/28 PHP
Ubuntu上安装yaf扩展的方法
2018/01/29 PHP
PHP获取远程http或ftp文件的md5值的方法
2019/04/15 PHP
php设计模式之迭代器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
一个简单的js鼠标划过切换效果
2010/06/30 Javascript
奉献给JavaScript初学者的编写开发的七个细节
2011/01/11 Javascript
分享一个自己写的table表格排序js插件(高效简洁)
2011/10/29 Javascript
多种方式实现JS调用后台方法进行数据交互
2013/08/20 Javascript
js动态修改整个页面样式达到换肤效果
2014/05/23 Javascript
jQuery提交多个表单的小技巧
2014/07/27 Javascript
Javascript 拖拽雏形中的一些问题(逐行分析代码,让你轻松了拖拽的原理)
2015/01/23 Javascript
jQuery+Ajax+PHP+Mysql实现分页显示数据实例讲解
2015/09/27 Javascript
详解bootstrap的modal-remote两种加载方式【强化】
2017/01/27 Javascript
jquery获取链接地址和跳转详解(推荐)
2017/08/15 jQuery
JavaScript 自定义html元素鼠标右键菜单功能
2019/12/02 Javascript
全面解析Vue中的$nextTick
2020/12/24 Vue.js
[01:32]2014DOTA2西雅图邀请赛 CIS我们有信心进入正赛
2014/07/08 DOTA
[01:00:30]TFT vs VGJ.T Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
Python序列循环移位的3种方法推荐
2018/04/09 Python
python装饰器-限制函数调用次数的方法(10s调用一次)
2018/04/21 Python
python使用循环打印所有三位数水仙花数的实例
2018/11/13 Python
python对绑定事件的鼠标、按键的判断实例
2019/07/17 Python
关于多元线性回归分析——Python&amp;SPSS
2020/02/24 Python
英国标志性奢侈品牌:Burberry
2016/07/28 全球购物
Fnac西班牙官网:法国文化和电子产品零售商
2021/03/14 全球购物
英国运动风奢侈品购物网站:Maison De Fashion
2020/08/28 全球购物
NULL是什么,它是怎么定义的
2015/05/09 面试题
什么是触发器(trigger)? 触发器有什么作用?
2013/09/18 面试题
大学生专业个人学习的自我评价
2013/10/26 职场文书
专营店会计助理岗位职责
2013/11/29 职场文书
建筑工地门卫岗位职责
2014/04/30 职场文书
银行先进个人事迹材料
2014/05/11 职场文书
ktv好的活动方案
2014/08/17 职场文书
环境工程专业毕业生求职信
2014/09/30 职场文书
2014年司法所工作总结
2014/11/22 职场文书
考试作弊检讨书范文
2015/01/27 职场文书