前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >angularjs input上传图片前获取图片的Size

angularjs input上传图片前获取图片的Size

作者头像
MJ.Zhou
发布2018-01-04 16:31:37
1.6K0
发布2018-01-04 16:31:37
举报
文章被收录于专栏:.NET开发那点事.NET开发那点事

首先我们需要一个指令来追踪input的change。ngChage不适用input[file]。

代码语言:txt
复制
app.directive("fileread", [function () {
    return {
        scope: {
            selectedFile: "=",
            changed: '&'
        },
        link: function(scope, element, attributes) {
            element.bind("change", function(changeEvent) {
                scope.$apply(function() {
                    scope.selectedFile = changeEvent.target.files[0];
                    // or all selected files:
                    // scope.fileread = changeEvent.target.files;
                    console.log('file selected.');
                    if (scope.changed()) {
                        scope.changed()(scope.selectedFile);
                    }
                });
            });
        }
    };
}]);

然后在controller里定义file的变量跟change绑定的function。

代码语言:txt
复制
 $scope.showFileSelectBox = function () {
        $("#imgSelectInput").click();
    };
代码语言:txt
复制
    $scope.imageSelected = function(file) {
                var image;
 
                if (file) {
 
                    image = new Image();
 
                    image.onload = function () {
代码语言:txt
复制
                        $scope.editObj.Width = this.width;
                        $scope.editObj.Height = this.height;
                    };
 
                    image.src = $window.URL.createObjectURL(file);
 
                }
    };

然后是html

代码语言:txt
复制
 <button type="button"  ng-click="showFileSelectBox()">上传</button>
                            <input type="file" style="display: none" accept="image/*" fileread selectedfile="selectedImgFile" id="imgSelectInput" changed="imageSelected" />
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-10-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档