Angalarjs之上拉菜单(ActionSheet)实现使用拍照和选择图库的方式上传头像功能


说明

上拉菜单(ActionSheet)通过往上弹出的框,来让用户选择选项。
非常危险的选项会以高亮的红色来让人第一时间识别。你可以通过点击取消按钮或者点击空白的地方来让它消失。
注明:以上为引用,本文笔者拍照方式上传头像并未直接调用相机,而是采用file属性capture=”camera” 即可,详细见代码

  • 笔者主要实现使用拍照和选择图库的方式上传头像功能

开发准备

功能开发

引用action-sheet.js与action-sheet.css

    <link rel="stylesheet" type="text/css" href="css/action-sheet.css"/>
 <script>
        document.write("<s"+"cript  data-main='text/javascript' src='lh-bsweb/js/action-sheet.js?t="+new Date().getTime()+"'></scr"+"ipt>");
    </script>

ps: 使用document.write写入的方式引用js文件可以起到去缓存的效果

app.js代码片段

var myApp = angular.module('myApp',["ngRoute",'actionsheet','upHeadImageCtrl']);

ps: myApp 注册”ngRoute”,’actionsheet’,’upHeadImageCtrl’

UpHeadImageCtrl.js代码片段

controller('upHeadImageCtrl', ['$scope','$rootScope','$location','userService','$http',function ($scope,$rootScope,$location,userService,$http) {
        currentpage="bankcard";
        var token= userService.checkLgin();
        $rootScope.basepath=basepath;
        $rootScope.headTitle = $rootScope.title = "上传头像";
        $scope.goback = function () {
            history.back();
        };
        //上拉菜单
        $scope.open=function(){
            var as = new ActionSheet({
                buttons: {
                    '拍照': function(e){
                        //if(!confirm('确定吗?')) return;
                        $("#cameraup").click();
                        this.hide();
                    },
                    '照片图库': function(e){
                        $("#fileup").click();
                        //if(!confirm('确定吗?')) return;
                        this.hide();
                    }
                    /* '跳转到百度': 'http://baidu.com/',*/
                }
            });
            as.show();
        };
        $scope.reader = new FileReader(); //创建一个FileReader接口
        $scope.myFile=null;
        $scope.thumb = {};      //用于存放图片的base64
     //头像上传
       $scope.headImageupload = function(files){
            $scope.myFile=null;
            $scope.myFile=files[0];
            $scope.submitUp();
            $scope.reader.readAsDataURL($scope.myFile);  //FileReader的方法,把图片转成base64
            $scope.reader.onload = function(ev) {
                $scope.$apply(function(){
                    $scope.thumb[0] = {
                        imgSrc : ev.target.result,  //接收base64
                    }
                });
            };
        };
        $scope.imgurl=basepath+'images/my/touxiang.png';
        $scope.thumb[0] = {
            imgSrc :   $scope.imgurl,  //接收base64
        };
        userService.getbusinessinfo({token:token}).then(function(data){
            if(data.code>0){
                $scope.imgurl=data.data[0].headurl;
                if( $scope.imgurl != null){
                    $scope.thumb[0] = {
                        imgSrc :   $scope.imgurl,  //接收base64
                    };
                }
            }
        });
        //头像上传
        var head_h5_upload_ops = {
            init:function(){
                this.eventBind();
            },
            eventBind:function(){
                $("#fileup").change(function(){
                    $scope.headImageupload(this.files);
                });
                $("#cameraup").change(function(){
                    $scope.headImageupload(this.files);
                });
            },
            compress : function (res) {

            }
        };
        head_h5_upload_ops.init();
        $scope.submitUp = function(){
            var data = new FormData();      //以下为像后台提交图片数据
            data.append('headimage', $scope.myFile);
            data.append('token',token);
            $http({
                method: 'post',
                url: baseurl+'/user/upheadimage',
                data:data,
                headers: {'Content-Type': undefined},
                transformRequest: angular.identity
            }).success(function(data) {
                console.log(data);
            })
        };
    }])

uploadheadimage.html代码片段

  <!--<input type="button" value="ActionSheet"/>-->
    <form  enctype="multipart/form-data">
        <div>
            <div ng-repeat="item in thumb" style="margin: auto;" align="center">
                <!-- 采用angular循环的方式,对存入thumb的图片进行展示 -->
                <span ng-if="item.imgSrc" ng-click="img_del($index)">
                    <img ng-src="{{item.imgSrc}}" style="width: 212px;height: 212px;margin-top: 20%;"/>
                </span>
            </div>
            <div style="display: none;">
                <label >
                    <input type="file"  id="fileup" accept="image/*" file-model="images" />
                    <input  type="file" id="cameraup" accept="image/*;" capture="camera"   />
                </label>
            </div>
        </div>
        <!--<input type="submit" name="" value="提 交" />-->
    </form>

效果展示

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值