samuelneff / MimeTypeMap

Provides a huge dictionary of file extensions to mime types.
MIT License
625 stars 201 forks source link

mp3 each browser return differnt mime type #30

Closed lettucebo closed 8 years ago

lettucebo commented 8 years ago

Today I encounter a strange behavior about matching mp3 mime type with browser return ContentType

and I have search an article: Which mime type should I use for mp3

Found out that each browser return different ContentType

if (!audio.ContentType.Equals(MimeTypeMap.GetMimeType(".mp3")))
{
    ViewBag.alert = "Incorrect,only accept *.mp3";
    return View(data);
}

using chrome will not be able to upload mp3 file

samuelneff commented 8 years ago

Mime types are unfortunately not 100% standard, even ones that are standard, not everyone follows the same standards. You can't assume that the mime type used in one place will match the exact mime type used elsewhere for the same files. Particularly look at the link you posted which shows some browsers using application/octet-stream which is a generic catch-all mime-type for binary even though zip is a well known file type.

Slightly better would be to check the file extension of the file uploaded, assuming it was provided. That should be .mp3. It's not fully safe though.. the user could rename any file to .mp3 to upload so you need to be aware of that depending on what you're later doing with the file.

Best would be to validate the contents of the upload are in fact an .mp3 audio. Example: http://stackoverflow.com/questions/7302439/how-can-i-determine-that-a-particular-file-is-in-fact-an-mp3-file

Good luck.