I want to use Javascript to prevent users from uploading any file except audio files. For example: MP3 only.
This code works on the desktop and some phones but on some older phones it gives an error when I upload MP3.
$("#track-file").change(function(e) {
var file = e.currentTarget.files[0];
if (file.type !== 'audio/mpeg') {
$('#track-file-error').html('* The file must be Audio');
$(this).val('');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="track-file" name="track-file" accept=".mp3">
file.typeof that file? E.g.console.log(file.type);typeis not supported in Firefox or Opera for Android. Could either of these be impacting your results? See MIME type of file returning empty in JavaScript on some machines.