Upload image js judge image size and format compatible with IE_javascript skills

Source: Internet
Author: User
This article mainly introduces how to upload an image. js judges the image size and format and is compatible with IE. If you need it, refer to the following JavaScript code:

$ (". Head"). change (function () {var val = $ (this). val (); if (! Val. match (/. jpg |. gif |. png |. bmp/I) {imgtype = false; alert ('invalid image format! ');} Else {if (FileReader) {var reader = new FileReader (), file = this. files [0]; reader. onload = function (e) {var image = new Image (); image. src = e.tar get. result; image. onload = function () {if (image. width & gt; 128 | image. height> 128) {fill = false; alert ("the Avatar size should be between X") ;}}; reader. readAsDataURL (file);} else {// This is ie9 version $ (". preview_size_fake "). show (); var objPreviewSizeFake = $ (". preview_size_fake "). get (0); var fileupload = $ (this ). get (0); fileupload. select (); fileupload. blur (); path = document. selection. createRange (). text; if (/"\ w \ W "/. test (path) {path = path. slice (1,-1);} objPreviewSizeFake. filters. item ('dximagetransform. microsoft. alphaImageLoader '). src = path; if (objPreviewSizeFake. offsetWidth> 128 | objPreviewSizeFake. offsetHeight> 128) {fill = false; alert ("the Avatar size should be between X");} document. selection. empty ();}}});

Css code (this must be written. If it is not written, it will not work in ie)

. Preview_size_fake {/* This object is only used to obtain the original size of the image under IE, and has no other purpose */filter: progid: DXImageTransform. microsoft. alphaImageLoader (sizingMethod = image); height: 1px; visibility: hidden; overflow: hidden; display: none ;}

Html code:

Related Article

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.