上传前检查图像大小 [英] checking image size before upload

查看:93
本文介绍了上传前检查图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在将个人资料图片上传到Twitter时,会在上传之前检查其大小。谁能指点我这样的解决方案?

I noticed when uploading a profile pic to Twitter that its size is checked before upload. Can anyone point me to a solution like this?

谢谢

推荐答案

我已经在chrome中测试了这个代码并且工作正常。

I have tested this code in chrome and it works fine.

var x = document.getElementById('APP_LOGO'); // get the file input element in your form
var f = x.files.item(0); // get only the first file from the list of files
var filesize = f.size;
alert("the size of the image is : "+filesize+" bytes");

var i = new Image();
var reader = new FileReader();
reader.readAsDataURL(f);
i.src=reader.result;
var imageWidth = i.width;
var imageHeight = i.height;
alert("the width of the image is : "+imageWidth);
alert("the height of the image is : "+imageHeight);

这篇关于上传前检查图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆