Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict file type for a upload, ie only to show mp3

Tags:

html

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.

like image 655
iamother Avatar asked Feb 25 '13 14:02

iamother


People also ask

How do I only accept certain file types?

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.

How do I change the file types in the upload field?

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.

How do you validate a file type in HTML?

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.


2 Answers

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">
like image 126
Konerak Avatar answered Oct 13 '22 04:10

Konerak


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.

like image 41
Subhi Jain Avatar answered Oct 13 '22 05:10

Subhi Jain