AngularJs上傳前預(yù)覽圖片的實(shí)例代碼
在工作中,使用AngularJs進(jìn)行開發(fā),在項(xiàng)目中,經(jīng)常會(huì)遇到上傳圖片后,需在一旁預(yù)覽圖片內(nèi)容,之前查了一些資料,結(jié)合實(shí)踐,得出一種比較實(shí)用的方法,相對簡化版,在這里記錄一下,如有不同看法,歡迎一起溝通,一起成長。
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">//關(guān)鍵 js 部分
var myTestCtrl = angular.module('myTestCtrl', []);
//定義“上傳”指令,修改后也可用于上傳其他類型的文件
myTestCtrl.directive("imgUpload",function(){
return{
//通過設(shè)置項(xiàng)來定義
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 與接口字段相對應(yīng)。
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(); //創(chuàng)建一個(gè)FileReader接口
console.log(scope.reader);
if (scope.file) {
//獲取圖片(預(yù)覽圖片)
scope.reader.readAsDataURL(scope.file[0]); //FileReader的方法,把圖片轉(zhuǎn)成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) {
//導(dǎo)入圖片
$scope.saveClick = function () {
//禁用按鈕
$scope.imgDisabled = true;
$scope.submitDisabled = true;
var url = '';//接口路徑
var fd = new FormData();
fd.append('testReport', $scope.file[0]);//參數(shù) testReport=后臺(tái)定義上傳字段名稱 ; $scope.file[0] 內(nèi)容
$http.post(url, fd, {
transformRequest: angular.identity,
headers: {
'Content-Type': undefined
}
}).success(function (data) {
if(data.code != 100) {
alert(JSON.stringify('文件導(dǎo)入失?。?+files.files[0].name+',請重新上傳正確的文件或格式'));
}else{
alert(JSON.stringify('文件導(dǎo)入成功:'+files.files[0].name));
}
//恢復(fù)按鈕
$scope.imgDisabled = false;
$scope.submitDisabled = false;
}).error(function (data) {
alert('服務(wù)器錯(cuò)誤,文件導(dǎo)入失敗!');
//恢復(fù)按鈕
$scope.imgDisabled = false;
$scope.submitDisabled = false;
});
};
});
</pre>
<br>
<pre></pre>
<p></p>
<pre>
</pre>
<p></p>
<pre>
</pre>
關(guān)于angularjs的知識(shí)大家可以參考下小編給大家整理的專題,angularjs學(xué)習(xí)筆記,一起看看吧!
以上所述是小編給大家介紹的AngularJs上傳前預(yù)覽圖片的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- AngularJS實(shí)現(xiàn)圖片上傳和預(yù)覽功能的方法分析
- Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能
- angularjs點(diǎn)擊圖片放大實(shí)現(xiàn)上傳圖片預(yù)覽
- angularjs實(shí)現(xiàn)多張圖片上傳并預(yù)覽功能
- 學(xué)習(xí)使用AngularJS文件上傳控件
- angularjs客戶端實(shí)現(xiàn)壓縮圖片文件并上傳實(shí)例
- AngularJS 文件上傳控件 ng-file-upload詳解
- 通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例
- SpringMvc+Angularjs 實(shí)現(xiàn)多文件批量上傳
- Angularjs實(shí)現(xiàn)多圖片上傳預(yù)覽功能
相關(guān)文章
Angular實(shí)現(xiàn)預(yù)加載延遲模塊的示例
本篇文章主要介紹了Angular實(shí)現(xiàn)預(yù)加載延遲模塊的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
AngularJS獲取json數(shù)據(jù)的方法詳解
這篇文章主要介紹了AngularJS獲取json數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了AngularJS獲取json數(shù)據(jù)的詳細(xì)步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05
基于AngularJS實(shí)現(xiàn)的工資計(jì)算器實(shí)例
這篇文章主要介紹了基于AngularJS實(shí)現(xiàn)的工資計(jì)算器,結(jié)合具體實(shí)例形式分析了AngularJS數(shù)值計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
AngularJs篇:使用AngularJs打造一個(gè)簡易權(quán)限系統(tǒng)的實(shí)現(xiàn)代碼
本篇文章主要介紹了AngularJs篇:使用AngularJs打造一個(gè)簡易權(quán)限系統(tǒng)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
詳解使用KeyValueDiffers檢測Angular對象的變化
這篇文章主要為大家介紹了KeyValueDiffers檢測Angular對象的變化使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
詳解Angular中的自定義服務(wù)Service、Provider以及Factory
本篇文章主要介紹了詳解Angular中的自定義服務(wù)Service、Provider以及Factory,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Bootstrap + AngularJS 實(shí)現(xiàn)簡單的數(shù)據(jù)過濾字符查找功能
這篇文章主要介紹了 Bootstrap + AngularJS 實(shí)現(xiàn)簡單的數(shù)據(jù)過濾字符查找功能,代碼簡單易懂,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07
angular+ionic 的app上拉加載更新數(shù)據(jù)實(shí)現(xiàn)方法
這篇文章主要介紹了angular+ionic 的app上拉加載更新數(shù)據(jù)實(shí)現(xiàn)方法,需要的的朋友參考下2017-01-01

