栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

上传之前可以检查图像尺寸吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

上传之前可以检查图像尺寸吗?

您可以在提交表单之前检查它们:

window.URL = window.URL || window.webkitURL;$("form").submit( function( e ) {    var form = this;    e.preventDefault(); //Stop the submit for now          //Replace with your selector to find the file input in your form    var fileInput = $(this).find("input[type=file]")[0],        file = fileInput.files && fileInput.files[0];    if( file ) {        var img = new Image();        img.src = window.URL.createObjectURL( file );        img.onload = function() { var width = img.naturalWidth,     height = img.naturalHeight; window.URL.revokeObjectURL( img.src ); if( width == 400 && height == 300 ) {     form.submit(); } else {     //fail }        };    }    else { //No file was input or browser doesn't support client side reading        form.submit();    }});

这仅适用于现代浏览器,因此您仍然必须在服务器端检查尺寸。您也不能信任客户端,因此这是您仍然必须在服务器端检查它们的另一个原因。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号