0

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">

5
  • 1
    And what error is that? Commented Dec 8, 2020 at 19:47
  • This means that the same error that I bet is displayed and says that the file is not MP3 while the file is MP3 Commented Dec 8, 2020 at 19:50
  • What is the file.type of that file? E.g. console.log(file.type); Commented Dec 8, 2020 at 20:05
  • 1
    Because console.log is not inside the mobile. I showed the file type inside a paragraph that plays audio / mpeg on the desktop, but inside the mobile it is null Commented Dec 8, 2020 at 20:28
  • This might be relevant: "Client configuration (for instance, the Windows Registry) may result in unexpected values even for common types. Developers are advised not to rely on this property as a sole validation scheme," (File.type). Also, type is 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. Commented Dec 8, 2020 at 20:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.