Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript checking if file is mp3-format?

Tags:

javascript

How do I do this? Something that begins with

if(file."something?"
like image 458
Himmators Avatar asked Sep 10 '26 03:09

Himmators


2 Answers

If you simply want to check that a filename ends in ".mp3" you can do the following.

// Assuming variable "file" is the filename
if(file.indexOf(".mp3") == file.length - 4) {
    // File type is .mp3
}

If you want to be doubly sure that the filename represents an actual mp3 file, you could send an XMLHttpRequest to the server and retrieve the HEAD, reading the Content-type.

Although your question seems a bit ambiguous to what "file" is ... It looks like file could be a custom type that represents a handler in an API or such, and in that case, the API documentation should help you out.

like image 145
Greg Avatar answered Sep 12 '26 16:09

Greg


Detecting a MIME type based on a file extension is unreliable. Don't.

Try sending an HTTP HEAD request using AJAX, then get the Content-Type returned.

like image 22
bcosca Avatar answered Sep 12 '26 16:09

bcosca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!