angularjs 实现图片上传实时预览
2017-04-18 09:22
597 查看
第一部分: html部分
angularjs 主要定义了 upload-img 指令,然后还有用到ng-src, 以及angularjs必须的文件
完整版下载网址:http://download.csdn.net/detail/u012767607/9817358
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="app">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/special.css" rel="stylesheet" />
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/special.js"></script>
<script src="js/angular-1.3.0.js"></script>
<script src="js/app.js"></script>
<script src="js/app/uploadPhoto.js"></script>
</head>
<body>
<div class="sp-page-content" ng-controller="addPhotoCtrl as ctl">
<div class="sp-page-box">
<div class="sp-page-boxcolumn span1">
照片:
</div>
<div class="sp-page-boxcolumn span2">
<span class="sp-upload">
<img class="sp-upload-photo" alt="" ng-src="{{ctl.info.photo1}}" />
<input type="file" upload-img class="sp-upload-img" />
</span>
</div>
</div>
<br />
<input type="button" value="打 印" ng-click="ctl.print()" class="sp-btn-blue" />
</div>
</body>
</html>
效果图:
![](https://img-blog.csdn.net/20170418092157835?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjc2NzYwNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
第二部分:js部分
2.1. app.js部分
'use strict';
var app = angular.module('app', [
]);
2.2. /uploadPhoto.js 部分
app.controller("addPhotoCtrl", ["$http", function ($http) {
var self = this;
self.info = {
photo1: "",
photo2: ""
};
self.print = function () {
console.log(self.info);
};
}]);
app.directive("uploadImg", function () {
return {
restrict: 'AE',
scope: false,
link: function (scope, elem, attrs) {
elem.bind('click', function () {
$(this).val('');
});
elem.change(function () {
var file = this.files[0];
if (file.size > 52428800) {
alert("图片大小不大于50M");
file = null;
return false;
}
var fileName = file.name;
var postfix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if (postfix != "jpg" && postfix != "png") {
alert("图片仅支持png、jpg类型的文件");
fileName = "";
file = null;
return false;
}
var fileUrl = $(this).val();
$(this).parent().children(".sp-upload-photo").attr("data-url", fileUrl);
var getimg = $(this).parent().children(".sp-upload-photo");
var filereader = new FileReader();
filereader.readAsDataURL(file);
$(filereader).load(function () {
getimg.attr("src", this.result);
console.log(this.result);
});
});
}
}
});
angularjs 主要定义了 upload-img 指令,然后还有用到ng-src, 以及angularjs必须的文件
完整版下载网址:http://download.csdn.net/detail/u012767607/9817358
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="app">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/special.css" rel="stylesheet" />
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/special.js"></script>
<script src="js/angular-1.3.0.js"></script>
<script src="js/app.js"></script>
<script src="js/app/uploadPhoto.js"></script>
</head>
<body>
<div class="sp-page-content" ng-controller="addPhotoCtrl as ctl">
<div class="sp-page-box">
<div class="sp-page-boxcolumn span1">
照片:
</div>
<div class="sp-page-boxcolumn span2">
<span class="sp-upload">
<img class="sp-upload-photo" alt="" ng-src="{{ctl.info.photo1}}" />
<input type="file" upload-img class="sp-upload-img" />
</span>
</div>
</div>
<br />
<input type="button" value="打 印" ng-click="ctl.print()" class="sp-btn-blue" />
</div>
</body>
</html>
效果图:
第二部分:js部分
2.1. app.js部分
'use strict';
var app = angular.module('app', [
]);
2.2. /uploadPhoto.js 部分
app.controller("addPhotoCtrl", ["$http", function ($http) {
var self = this;
self.info = {
photo1: "",
photo2: ""
};
self.print = function () {
console.log(self.info);
};
}]);
app.directive("uploadImg", function () {
return {
restrict: 'AE',
scope: false,
link: function (scope, elem, attrs) {
elem.bind('click', function () {
$(this).val('');
});
elem.change(function () {
var file = this.files[0];
if (file.size > 52428800) {
alert("图片大小不大于50M");
file = null;
return false;
}
var fileName = file.name;
var postfix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if (postfix != "jpg" && postfix != "png") {
alert("图片仅支持png、jpg类型的文件");
fileName = "";
file = null;
return false;
}
var fileUrl = $(this).val();
$(this).parent().children(".sp-upload-photo").attr("data-url", fileUrl);
var getimg = $(this).parent().children(".sp-upload-photo");
var filereader = new FileReader();
filereader.readAsDataURL(file);
$(filereader).load(function () {
getimg.attr("src", this.result);
console.log(this.result);
});
});
}
}
});
相关文章推荐
- JS实现上传图片实时预览功能
- SpringMVC实现ajax上传图片实时预览
- Angularjs实现上传图片预览功能
- angularjs实现多张图片上传并预览功能
- js实现上传图片实时预览,兼容chrome IE firefox多浏览器
- Angularjs实现多图片上传预览功能
- js实现上传图片实时预览
- AngularJS实现图片上传和预览功能的方法分析
- JS实现上传图片实时预览
- Jquery实现上传图片时可以实时预览
- Nginx + image_filter_module + fastdfs实现上传图片的实时缩略图预览
- Angularjs 实现多图片上传预览
- 前端实现图片上传实时预览
- 利用jquery,html5实现图片预览实时上传
- js实现上传图片实时预览
- [AS3]纯AS代码实现可预览本地图片的flash上传客户端
- 可预览缩略图(实时预览)的上传图片界面
- 用js实现上传图片前的预览
- [转]FF3(FireFox3),IE8上传图片实现图片预览的方法
- js实现IE7图片上传预览的功能代码