HTML5 type=file檔案上傳功能執行個體詳解

來源:互聯網
上載者:User
本文主要和大家介紹HTML5新特性之type=file檔案上傳功能,需要的朋友可以參考下,希望能協助到大家。

1、文法

<input name="myFile" type="file">

2、屬性(以下三個僅HTML5支援,因此存在相容性問題)

(1)multiple :表示使用者是否可以選擇多個值。multiple只能用於type=file和type=email。

(2)accept:伺服器接受的檔案類型,否則將被忽略。

音頻/ *代表音效檔。僅HTML5支援
視頻/ *代表視頻檔案。僅HTML5支援
映像/ *表示影像檔。僅HTML5支援

(3)required:此屬性指定使用者在提交表單之前必須填寫一個值。

3、擷取上傳的檔案資訊

<!DOCTYPE html><html lang="zh">    <head>        <meta charset="UTF-8" />        <meta http-equiv="X-UA-Compatible" content="ie=edge" />        <title>file multiple</title>    </head>    <body>        <input type="file" multiple="multiple" id="test">        <p id='content'></p>        <script type="text/javascript">            var test = document.getElementById('test');            test.addEventListener('change', function() {                var t_files = this.files;                var str = '';                for(var i = 0, len = t_files.length; i < len; i++) {                    console.log(t_files[i]);                    str += '<a href="javascript:void(0)">' + t_files[i].name + '</a><br/>';                };                document.getElementById('content').innerHTML = str;            }, false);        </script>    </body></html>

顯示:

相關文章

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.