Himmators
Himmators

Reputation: 15016

Javascript checking if file is mp3-format?

How do I do this? Something that begins with

if(file."something?"

Upvotes: 3

Views: 2522

Answers (2)

bcosca
bcosca

Reputation: 17555

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.

Upvotes: 3

Greg
Greg

Reputation: 21909

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.

Upvotes: 6

Related Questions