HTML 中的 enctype='multipart/form-data' 意味著什麼?


使用 enctype 屬性指定瀏覽器在將資料傳送到伺服器之前如何對其進行編碼。可能的值有:-

  • application/x-www-form-urlencoded —這是大多數表單在簡單場景中使用的一種標準方法。

  • mutlipart/form-data —當你要以檔案形式上傳二進位制資料,例如影像、Word 檔案等,就使用此方法。

示例

現在讓我們來看一個例子:-

<!DOCTYPE html> <html> <head> <title>HTML enctype attribute</title> <style> form { width: 70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin: 5px; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <h1>Login</h1> <form enctype="multipart/form-data" action="" method="post"> <fieldset> <legend>Enter the login details</legend> <label for="EmailSelect">Email Id: <input type="email" id="EmailSelect"> <input type="button" onclick="getUserEmail('abc')" value="abc"> <input type="button" onclick="getUserEmail('pqr')" value="pqr"><br> <input type="button" onclick="login()" value="Login"> </label> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var inputEmail = document.getElementById("EmailSelect"); function getUserEmail(userName) { if (userName === 'abc') inputEmail.value = 'abc@MNC.com'; else inputEmail.value = 'pqr@MNC.com'; } function login() { if (inputEmail.value !== '') divDisplay.textContent = 'Successful Login. Hello ' + inputEmail.value.split("@")[0]; else divDisplay.textContent = 'Enter Email Id'; } </script> </body> </html>

登入並顯示結果:-

更新於:2022 年 10 月 18 日

2K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告