【问题标题】:On image upload get dimension(width and height) validation using angularjs在图像上传时使用 angularjs 获取尺寸(宽度和高度)验证
【发布时间】:2017-09-26 13:43:49
【问题描述】:

嗨,我是 Angular JS 的新手。我一直在尝试做某事,我有一个登录表单,其中我有一个文件输入类型,用户可以在其中上传他的图片。输入框是这样的

<input type="file" file-model="userPic"/>

所以,在controller.js文件中提交表单的时候我

console.log(userPic)

我得到文件名、大小、修改日期。但是我想在表单提交之前找到上传图片宽度和高度的尺寸。我还安装了凉亭组件,我有 jquery 文件夹,里面有 dimensions.js 文件,有用吗?

我也尝试过使用 .width() 来 console.log。为此我稍微更改了代码

<input type="file" file-model="userPic" id="userId"/>

并在表单提交的controller.js文件中

console.log($('#userId').width());

我已经尝试过了,但我得到的输出不正确。我将如何进行。

【问题讨论】:

标签: javascript angularjs


【解决方案1】:

您需要使用 naturalWidth 和 naturalHeight:

$scope.userPic.naturalHeight();
$scope.userPic.naturalWidth();

【讨论】:

  • 我不能使用文件模型值,我的意思是 console.log(userPic.naturalWidth());而不是使用 id。
  • 您可能需要使用 $scope.userPic 才能使其工作。
  • @William TypeError: $scope.userPic.naturalWidth() 不是函数我收到此错误
  • @user3270582 in javascript naturalWidth 是一个属性,JQuery 将它作为一个函数来管理,我担心 Angularjs 没有。因此,请尝试将其用作属性,删除末尾的 () 括号。
  • @MarioZ 如果我按照您在未定义中的建议删除末尾的 () 括号。
猜你喜欢
  • 1970-01-01
  • 2012-01-19
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 2011-12-12
  • 2013-07-16
  • 1970-01-01
相关资源
最近更新 更多