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

最新下载

热门教程

asp.net+jquery上传图片插件及图片上传格式像素限制

时间:2014-10-01 编辑:简简单单 来源:一聚教程网

今天分享一下怎么对上传文件类型进行过滤,以及对图片像素的判断处理。

1、在html中实现对文件类型的过滤

所有上传插件都是基于iframe+file input控件实现,除了flash和siverlight的控件方式的。

file input的时候是支持对文件类型进行过滤的。过滤类型是:

word" >

accept属性列表

1.accept="application/msexcel"
2.accept="application/msword"
3.accept="application/pdf"
4.accept="application/poscript"
5.accept="application/rtf"
6.accept="application/x-zip-compressed"
7.accept="audio/basic"
8.accept="audio/x-aiff"
9.accept="audio/x-mpeg"
10.accept="audio/x-pn/realaudio"
11.accept="audio/x-waw"
12.accept="image/gif"
13.accept="image/jpeg"
14.accept="image/tiff"
15.accept="image/x-ms-bmp"
16.accept="image/x-photo-cd"
17.accept="image/x-png"
18.accept="image/x-portablebitmap"
19.accept="image/x-portable-greymap"
20.accept="image/x-portable-pixmap"
21.accept="image/x-rgb"
22.accept="text/html"
23.accept="text/plain"
24.accept="video/quicktime"
25.accept="video/x-mpeg2"
26.accept="video/x-msvideo"
这个就可以实现选择具体的数据类型,但是有兼容问题,我在测试的时候只有Opera,Chrome能用,火狐和IE都不兼容
好吧,就在这里测试一下吧:
我只想要word
在这里如果想支持多种类型的话,比如金山的office和microsoft的office的不同扩展名,这样的话可以在accept里面放置多个属性就可以了:


如果不限制图像的格式,可以写为:accept="image/*"。同样是可以的
好的,测试一下:
我只想要gif、bmp
好吧,只要是图片我就要了
嗯,测试成功,用Opera,Chrome能用,因为accept也是html5的新特性,所以火狐和IE的支持就显得单薄了,这样还是让我们等待他们的接受把
测试了好久,发现gif和bmp一起的时候并不是能取得他们的并集,仔细观察以后是按写入顺序排列了一下他们的“文件选择类型”:
就像是下面的图片一样:
IT分享
我们如果不定义input file的格式的时候,那么文件类型的地方就是全部文件*.*,如果我们定义了自己的,那么文件类型就会和我们选中的一样,但是这时候我们如果想看别的文件的时候还是可以通过文件类型的下拉框,去选择自己额外喜欢的类型的,简单的说,它就是起到了一个简单的按照我们的主观意愿筛选的作用,同时我们如果不是选择的image/*而是好几个并列的话就会按照上面的情况分别得排列出来,比如图上的我input就是这个样子的:

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;
        }

像素判断:

 代码如下 复制代码

string s = @"C:\Users\nonocast.DEV.000\Desktop\bird.jpg";
var sh = new Shell32.ShellClass();
Shell32.Folder folder = sh.NameSpace(Path.GetDirectoryName(s));
Shell32.FolderItem item = folder.ParseName(@"bird.jpg");
var sizestr = folder.GetDetailsOf(item, 31);
Regex r = new Regex(@"(\d+)[^\d]+(\d+)");
if(r.IsMatch(sizestr)){
    var m = r.Match(sizestr);
    Console.WriteLine(Convert.ToInt32(m.Groups[1].Value));
    Console.WriteLine(Convert.ToInt32(m.Groups[2].Value));
}

热门栏目