一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

jquery plupload上传图片尺寸验证的方法

时间:2022-06-25 17:29:04 编辑:袖梨 来源:一聚教程网

plupload是很强大的一个上传组件,支持html5,flash,silverlight,html4等语言插件。本文分享2个验证上传图片大小的方法。

1、浏览器客户端验证:

在支持 FileReader 的浏览器上可以做上传前校验,否则只能在上传后校验了

支持 FileReader 的浏览器上代码:


FilesAdded: function (up, files) {
        var reader = new FileReader();
        reader.readAsDataURL(files[0].getNative());
        reader.onload = (function (e) {
            var image = new Image();
            image.src = e.target.result;
            image.onload = function () {
                if (this.width < 1000 && this.height < 1000) {
                    uploader.start();
                } else {
                    console.log('尺寸不符')
                }
            };
        });
    }


2、服务器端验证(C#例子)


public static bool CheckFileTypeLimit(string type,string filepath)
        {
            bool isok = false;
            if (File.Exists(filepath))
            {
                FileInfo fileinfo = new FileInfo(filepath);
                switch (type)
                {
                    case "image"://大小: 不超过1M,    格式: bmp, png, jpeg, jpg, gif
                        if (  fileinfo.Extension == ".JPG"
                                  || fileinfo.Extension == ".jpg"  
                            && fileinfo.Length / 1024 / 1024 < 1
                            )
                        {
                            isok = true;
                        }
                        break;
                    case "voice"://大小: 不超过5M, 长度:不超过60s, 格式: mp3, wma, wav, amr
                        if (( fileinfo.Extension == ".amr")
                         && fileinfo.Length / 1024/1024 < 2
                         )
                        {
                            isok = true;
                        }
                        break;
                    case "video"://大小: 不超过20M, 格式: rm, rmvb, wmv, avi, mpg, mpeg, mp4
                        if ((  fileinfo.Extension == ".mp4"
                      )
                       && fileinfo.Length / 1024 / 1024 < 10
                       )
                        {
                            isok = true;
                        }
                        break;
                    case "file"://大小: 不超过10M,    格式: txt, xml, pdf, zip, doc, ppt, xls, docx, pptx, xlsx
                        if ((fileinfo.Extension == ".txt"
                       || fileinfo.Extension == ".xml"
                             || fileinfo.Extension == ".pdf" || fileinfo.Extension == ".zip" || fileinfo.Extension == ".doc"
                             || fileinfo.Extension == ".ppt" || fileinfo.Extension == ".docx" || fileinfo.Extension == ".pptx"
                                          || fileinfo.Extension == ".xlsx"
                             || fileinfo.Extension == ".xls")
                       && fileinfo.Length / 1024 / 1024 < 10
                       )
                        {
                            isok = true;
                        }
                        break;
                    default:
                        break;
                }
            }
            return isok;
        }

第一个例子在前段验证, 例子二是在后段验证了

热门栏目