Asp.net + jquery image Upload plugin and image Upload format pixel restrictions

Source: Internet
Author: User

Today, I will share with you how to filter uploaded files and determine the image pixels.

1. Filter file types in html

All Upload plug-ins are implemented based on the iframe + file input control, except for the flash and siverlight control methods.

File input supports filtering file types. The filter type is:

<Input type = "file" accept = "application/msword"> <br> accept Attribute List <br>
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"
This allows you to select a specific data type, but there is a compatibility problem. During the test, only Opera and Chrome can be used, and Firefox and IE are not compatible.
Okay, let's test it here:
I only want word
If you want to support a variety of types, such as different extensions of office and microsoft office in Kingsoft, you can place multiple attributes in accept:
<Form> <input type = "file" name = "pic" id = "pic" accept = "image/gif, image/jpeg"/> </form>
If you do not limit the image format, you can enter: accept = "image /*". The same is true.
Okay. Let's test:
I only want gif, bmp
Okay, as long as it's an image, I need it.
Well, the test is successful. It can be used with Opera and Chrome. Because accept is also a new feature of html5, Firefox and IE support is thin, which makes us wait for their acceptance.
After testing for a long time, we found that gif and bmp were not able to obtain their union. After careful observation, they arranged their "file selection type" in the order of writing ":
Just like the image below:
IT sharing
If the format of the input file is not defined, the file type is all files *. * if we define our own file type, the file type will be the same as the one we selected. However, if we want to see other files, we can still use the file type drop-down box, simply put, choosing an extra type you like plays a simple role of filtering based on our subjective desires, at the same time, if we do not select the image/* but have several parallel items, we will sort them according to the above situations. For example, my input is like this:
<Form> <input type = "file" name = "pic" id = "pic" accept = "image/gif, image/x-ms-bmp, image/bmp "/> </form>
2. Perform image size verification and pixel determination in C #.

The code is as follows: Copy code


Public static bool CheckFileTypeLimit (string type, string filepath)
        {
Bool isok = false;
If (File. Exists (filepath ))
            {
FileInfo fileinfo = new FileInfo (filepath );
Switch (type)
                {
Case "image": // Size: up to 1 MB. Format: bmp, png, jpeg, jpg, gif
If (fileinfo. Extension = ". JPG"
| Fileinfo. Extension = ". jpg"
& Amp; fileinfo. Length/1024/1024 <1
                            )
                        {
Isok = true;
                        }
Break;
Case "voice": // Size: no more than 5 M, length: no more than 60 s, format: mp3, wma, wav, amr
If (fileinfo. Extension = ". amr ")
& Amp; fileinfo. Length/1024/1024 <2
                         )
                        {
Isok = true;
                        }
Break;
Case "video": // Size: no more than 20 MB. Format: rm, rmvb, wmv, avi, mpg, mpeg, mp4
If (fileinfo. Extension = ". mp4"
                      )
& Fileinfo. Length/1024/1024 <10
                       )
                        {
Isok = true;
                        }
Break;
Case "file": // Size: up to 10 MB. Format: 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;
        }

Pixel judgment:

The code is as follows: Copy code

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.