Is it possible to restrict the file upload for an form input to be only show files with the extension of mp3, like this:
<input class="someClass" type="file" name="UploadAudio" accept="audio/mp3"/>
Because for some reason this does not work, it always seems to show other file type extensions for upload.
Acceptable file types can be specified with the accept attribute, which takes a comma-separated list of allowed file extensions or MIME types. Some examples: accept="image/png" or accept=".png" — Accepts PNG files. accept="image/png, image/jpeg" or accept=".png, .jpg, .jpeg" — Accept PNG or JPEG files.
Click on the gear icon to the right of the File upload field to open the File Upload Properties panel. Go to the Options tab. Enter the allowed file extensions in the File Types field.
Using JavaScript, you can easily check the selected file extension with allowed file extensions and can restrict the user to upload only the allowed file types. For this we will use fileValidation() function. We will create fileValidation() function that contains the complete file type validation code.
To specify an extension, start with the dot:
<input type="file" accept=".mp3,audio/*">
This will accept all files that have extension mp3 OR files of the Audio type. The comma allows to list multiple possibilities, so just remove the ,audio
if you strictly only want .mp3
(and not .wav
for example).
Your audio/mp3 is mixing both ways: either you use the extension OR the correct mime type, which should be audio/mpeg3
So your possibilities are:
<input type="file" accept=".mp3">
or
<input type="file" accept="audio/mpeg3">
To accept all kind of audio files, write all type of accepted extensions explicitly.
<input type="file" accept="audio/mp3,audio/*;capture=microphone" />
this also hides the choose file button displayed on the screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With