socialforge/public/javascripts/wechat/directives/load_head.js

35 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-09-07 14:19:07 +08:00
app.directive('loadHead',['$http','config','auth','$location','alertService',function($http,config,auth,$location,alertService){
return{
restrict: 'A',
scope: {},
link: function (scope, element, attrs) {
element.bind('change', function(){
var file = event.target.files[0];
//判断类型是不是图片
if (!/image\/\w+/.test(file.type)) {
alert("非图片");
return;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
2016-09-07 16:16:53 +08:00
// alert(this.result);//base64
2016-09-07 14:19:07 +08:00
$http.post(config.apiUrl + "users/upload_head",
{token: auth.token(),imgdata:this.result}
).then(function (response) {
if(response.data.status == 0){
scope.$parent.alertService_1.showMessage('提示',"上传成功",function(){
scope.$parent.getuserinfo();
});
}
else{
2016-09-07 16:16:53 +08:00
scope.$parent.alertService_1.showMessage('提示',response.data.message);
2016-09-07 14:19:07 +08:00
}
});
}
});
}
}
}]);