javascript圖片上傳格式尺寸等特徵驗證效果
本章節分享一段程式碼例項,它實現了驗證圖片上傳格式或者尺寸等特性的功能。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> UpLoadFileCheck=function(){ this.AllowExt=".jpg,.gif"; //允許上傳的檔案型別 0為無限制 //每個副檔名後邊要加一個"," 小寫字母表示 this.AllowImgFileSize=0; //允許上傳檔案的大小 0為無限制 單位:KB this.AllowImgWidth=0; //允許上傳的圖片的寬度 0為無限制 單位:px(畫素) this.AllowImgHeight=0; //允許上傳的圖片的高度 0為無限制 單位:px(畫素) this.ImgObj=new Image(); this.ImgFileSize=0; this.ImgWidth=0; this.ImgHeight=0; this.FileExt=""; this.ErrMsg=""; this.IsImg=false;//全域性變數 } UpLoadFileCheck.prototype.CheckExt=function(obj){ this.ErrMsg=""; this.ImgObj.src=obj.value; //this.HasChecked=false; if(obj.value==""){ this.ErrMsg="\n請選擇一個檔案"; } else{ this.FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase(); //判斷檔案型別是否允許上傳 if(this.AllowExt!=0&&this.AllowExt.indexOf(this.FileExt)==-1){ this.ErrMsg="\n該檔案型別不允許上傳。請上傳 "+this.AllowExt+" 型別的檔案,當前檔案型別為"+this.FileExt; } } if(this.ErrMsg!=""){ this.ShowMsg(this.ErrMsg,false); return false; } else return this.CheckProperty(obj); } UpLoadFileCheck.prototype.CheckProperty=function(obj){ if(this.ImgObj.readyState!="complete"){ sleep(1000);//一秒使用圖能完全載入 } if(this.IsImg==true){ this.ImgWidth=this.ImgObj.width; //取得圖片的寬度 this.ImgHeight=this.ImgObj.height; //取得圖片的高度 if(this.AllowImgWidth!=0&&this.AllowImgWidth<this.ImgWidth) this.ErrMsg=this.ErrMsg+"\n圖片寬度超過限制。請上傳寬度小於"+this.AllowImgWidth+"px的檔案,當前圖片寬度為"+this.ImgWidth+"px"; if(this.AllowImgHeight!=0&&this.AllowImgHeight<this.ImgHeight) this.ErrMsg=this.ErrMsg+"\n圖片高度超過限制。請上傳高度小於"+this.AllowImgHeight+"px的檔案,當前圖片高度為"+this.ImgHeight+"px"; } this.ImgFileSize=Math.round(this.ImgObj.fileSize/1024*100)/100; //取得圖片檔案的大小 if(this.AllowImgFileSize!=0&&this.AllowImgFileSize<this.ImgFileSize) this.ErrMsg=this.ErrMsg+"\n檔案大小超過限制。請上傳小於"+this.AllowImgFileSize+"KB的檔案,當前檔案大小為"+this.ImgFileSize+"KB"; if(this.ErrMsg!=""){ this.ShowMsg(this.ErrMsg,false); return false; } else return true; } //顯示提示資訊 tf=false 顯示錯誤資訊 msg-資訊內容 UpLoadFileCheck.prototype.ShowMsg=function(msg,tf){ /*msg=msg.replace("\n","<li>"); msg=msg.replace(/\n/gi,"<li>"); */ alert(msg); } function sleep(num){ var tempDate=new Date(); var tempStr=""; var theXmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); while((new Date()-tempDate)<num ){ tempStr+="\n"+(new Date()-tempDate); try{ theXmlHttp .open( "get", "about:blank?JK="+Math.random(), false ); theXmlHttp .send(); } catch(e){;} } //containerDiv.innerText=tempStr; return; } function c(obj){ var d=new UpLoadFileCheck(); d.IsImg=true; d.AllowImgFileSize=100; d.CheckExt(obj) } </script> </head> <body> <input name="" type="file" onchange="c(this)"/> </body> </html>
相關文章
- app直播原始碼,js實現上傳圖片型別+大小+尺寸驗證APP原始碼JS型別
- js獲取上傳圖片尺寸和格式程式碼例項JS
- kindeditor 上傳圖片 自動調整尺寸大小
- 圖片上傳-下載-刪除等圖片管理的若干經驗總結
- 圖片上傳-下載-刪除等圖片管理的若干經驗總結2
- javascript和HTML5上傳圖片之前實現預覽效果JavaScriptHTML
- 圖片格式型別驗證正規表示式型別
- 用JavaScript獲取原始圖片尺寸JavaScript
- 上傳圖片的時候好像受到了限制,請問上傳圖片的屬性如何設定,比如限制尺寸或容量,副檔名等
- JavaScript驗證碼生成和驗證效果JavaScript
- jQuery實現圖片尺寸自適應效果jQuery
- JavaScript郵箱格式驗證JavaScript
- 圖片上傳預覽效果程式碼例項
- SpringBoot如何驗證使用者上傳的圖片資源Spring Boot
- js驗證上傳圖片副檔名型別程式碼例項JS型別
- javascript如何獲取img圖片的尺寸JavaScript
- JavaScript 圖片放大鏡效果JavaScript
- js如何檢測上傳圖片的字尾格式JS
- Element-UI框架 —— Upload 上傳(圖片上傳格式和大小判斷)UI框架
- Retrofit+RxJava上傳圖片上傳圖片到後臺RxJava
- UEditor 自定義圖片視訊尺寸校驗
- 【easyui 】上傳圖片UI
- JavaScript 圖片切換展示效果JavaScript
- jQuery限制上傳圖片格式和大小程式碼例項jQuery
- C#上傳檔案圖片怎麼判斷格式C#
- 圖片上傳及圖片處理
- FileReader初步使用實現上傳圖片預覽效果
- 圖片驗證碼 CaptchaAPT
- 圖片驗證碼介面
- element上傳圖片元件使用方法|圖片回顯|格式轉換base64元件
- javascript檢測上傳圖片大小程式碼例項JavaScript
- php圖片上傳之圖片轉換PHP
- java,springboot + thymeleaf 上傳圖片、刪除圖片到伺服器、本地,壓縮圖片上傳(有些圖片會失真),原圖上傳JavaSpring Boot伺服器
- Canvas + JavaScript 製作圖片粒子效果CanvasJavaScript
- dcat-admin上傳圖片 會自動把圖片壓成2:1的格式?
- 相容所有瀏覽器的圖片上傳本地預覽效果瀏覽器
- html5拖動上傳圖片效果程式碼例項HTML
- 多圖片formpost上傳ORM