How do I do this? Something that begins with
if(file."something?"
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.
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.
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