HTML5的 input:file上傳類型控制

來源:互聯網
上載者:User

標籤:form   asi   圖片   XML   pps   bsp   html   tmp   room   

轉自:http://www.haorooms.com/post/input_file_leixing一、input:file屬性

屬性值有以下幾個比較常用:

accept:表示可以選擇的檔案MIME類型,多個MIME類型用英文逗號分開,常用的MIME類型見下表。

multiple:是否可以選擇多個檔案,多個檔案時其value值為第一個檔案的虛擬路徑。

1、accept

只能選擇png和gif圖片

<input id="fileId1" type="file" accept="image/png,image/gif" name="file" />
2、multiple

多檔案上傳

<input id="fileId2" type="file" multiple="multiple" name="file" />
3、常用MIME類型
尾碼名       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

三、AJAX上傳檔案

在說到ajax上傳檔案。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        ))

在服務端迴圈遍曆這個數組就可以上傳檔案了。

HTML5的 input:file上傳類型控制

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.