function test() { var filePath = $("#image").val(); if("" != filePath) { var fileType = getFileType(filePath); //console.log(fileType); //判斷上傳的附件是否爲圖片 if("jpg" != fileType && "jpeg" != fileType && "png" != fileType) { $("#image").val(""); alert("請上傳JPG,JPEG,PNG格式的圖片"); } else { //獲取附件大小(單位:KB) var fileSize = document.getElementById("image").files[0].size / 1024; if(fileSize > 2048) { alert("圖片大小不能超過2MB"); } } } } function getFileType(filePath) { var startIndex = filePath.lastIndexOf("."); if(startIndex != -1) return filePath.substring(startIndex + 1, filePath.length).toLowerCase(); else return ""; }html:
<input type="file" name="image" id="image" accept="image/x-png" onchange="test()" />
一、input 屬性值有如下幾個比較經常使用:javascript
accept:表示能夠選擇的文件MIME類型,多個MIME類型用英文逗號分開,經常使用的MIME類型見下表。php
multiple:是否能夠選擇多個文件,多個文件時其value值爲第一個文件的虛擬路徑。css
二、accepthtml
<input id="fileId1" type="file" accept="image/png,image/gif" name="file" /> 測試很差用 ??多個屬性不能夠?
多文件上傳java
<input id="fileId2" type="file" multiple="multiple" name="file" />
四、經常使用MIME類型ajax
後綴名 MIME名稱 *.3gpp audio/3gpp, video/3gpp *.ac3 audio/ac3 *.asf allpication/vnd.ms-asf *.au audio/basic *.css text/css *.csv text/csv *.doc application/msword *.dot application/msword *.dtd application/xml-dtd *.dwg image/vnd.dwg *.dxf image/vnd.dxf *.gif image/gif *.htm text/html *.html text/html *.jp2 image/jp2 *.jpe image/jpeg *.jpeg image/jpeg *.jpg image/jpeg *.js text/javascript, application/javascript *.json application/json *.mp2 audio/mpeg, video/mpeg *.mp3 audio/mpeg *.mp4 audio/mp4, video/mp4 *.mpeg video/mpeg *.mpg video/mpeg *.mpp application/vnd.ms-project *.ogg application/ogg, audio/ogg *.pdf application/pdf *.png image/png *.pot application/vnd.ms-powerpoint *.pps application/vnd.ms-powerpoint *.ppt application/vnd.ms-powerpoint *.rtf application/rtf, text/rtf *.svf image/vnd.svf *.tif image/tiff *.tiff image/tiff *.txt text/plain *.wdb application/vnd.ms-works *.wps application/vnd.ms-works *.xhtml application/xhtml+xml *.xlc application/vnd.ms-excel *.xlm application/vnd.ms-excel *.xls application/vnd.ms-excel *.xlt application/vnd.ms-excel *.xlw application/vnd.ms-excel *.xml text/xml, application/xml *.zip aplication/zip *.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
css input[type=file] 樣式美化,input上傳按鈕美化 http://www.haorooms.com/post/css_input_uploadmh
3、AJAX上傳文件json
ajax上傳的時候,須要得到input:file選擇的文件(可能爲多個文件),獲取其文件列表爲: // input標籤的files屬性 document.querySelector("#fileId").files // 返回的是一個文件列表數組 得到的文件列表,而後遍歷插入到表單數據當中。即: // 得到上傳文件DOM對象 var oFiles = document.querySelector("#fileId"); // 實例化一個表單數據對象 var formData = new FormData(); // 遍歷圖片文件列表,插入到表單數據中 for (var i = 0, file; file = oFiles[i]; i++) { // 文件名稱,文件對象 formData.append(file.name, file); } 得到表單數據以後,就能夠用ajax的POST上傳。 // 實例化一個AJAX對象 var xhr = new XMLHttpRequest(); xhr.onload = function() { alert("上傳成功!"); } xhr.open("POST", "upload.php", true); // 發送表單數據 xhr.send(formData); 上傳到服務器以後,獲取到文件列表爲: Array ( [jpg_jpg] => Array ( [name] => jpg.jpg [type] => image/jpeg [tmp_name] => D:\xampp\tmp\phpA595.tmp [error] => 0 [size] => 133363 ) [png_png] => Array ( [name] => png.png [type] => image/png [tmp_name] => D:\xampp\tmp\phpA5A6.tmp [error] => 0 [size] => 1214628 ) ) 在服務端循環遍歷這個數組就能夠上傳文件了。