Javascript DOM HTML Input FileUpload accept Property set

Introduction

Change the accepted content type:

// With the following setting,
// the server will only accept audio files in the file upload 
document.getElementById("myFile").accept = "audio/*";

Click the button below to change the value of the accept attribute of the file upload button.

View in separate window

<!DOCTYPE html>
<html>
<body>

Select a file to upload:
<input type="file" id="myFile" size="50" accept="image/*">

<button onclick="myFunction()">Change accepted file types</button>

<p id="demo"></p>

<script>
function myFunction() {/* w  w w  .j a va  2s .  c  o m*/
  document.getElementById("myFile").accept = "audio/*";
  document.getElementById("demo").innerHTML = "The accepted file types have been changed.";
}
</script>

</body>
</html>

The accept property sets or gets the value of the accept attribute of the file upload button.

The accept attribute specifies the types of files that the server accepts.

The accept property accepts and returns a String type value.

To specify more than one value, separate the values with a comma.

Value Description
audio/* All sound files are accepted
video/* All video files are accepted
image/* All image files are accepted
MIME_type A valid MIME type, with no parameters.

The accept property returns a string containing a comma-separated list of accepted content types.




PreviousNext

Related