File input give different Mimetype for the same file in chrome or firefox. I have a wav file that I want to upload, chrome says it is audio/wav
and firefox detect audio/x-wav
.
I know those two mimetype are very similar (x-
stands for non-standard), but why are they handled differently in this case?
Here is a fiddle to illustrate this: /. And here is the WAV file I used for this example: /.
In the end the behavior that I would like is to take a .wav
file from my puter (client) and send it by HTTP to my server as audio/wav
regardless of the browser.
There's a subsequent question to this: how to harmonize this behavior?
File input give different Mimetype for the same file in chrome or firefox. I have a wav file that I want to upload, chrome says it is audio/wav
and firefox detect audio/x-wav
.
I know those two mimetype are very similar (x-
stands for non-standard), but why are they handled differently in this case?
Here is a fiddle to illustrate this: https://jsfiddle/r9ae0zfd/. And here is the WAV file I used for this example: https://freesound/people/zagi2/sounds/391828/.
In the end the behavior that I would like is to take a .wav
file from my puter (client) and send it by HTTP to my server as audio/wav
regardless of the browser.
There's a subsequent question to this: how to harmonize this behavior?
Share Improve this question edited Jul 20, 2017 at 14:54 Ulysse BN asked Jul 3, 2017 at 17:28 Ulysse BNUlysse BN 11.4k7 gold badges62 silver badges93 bronze badges 13- The fiddle uses a file that you want to upload? – Bergi Commented Jul 18, 2017 at 16:15
-
2
Why do you need this mimeType ? Browsers only check for extensions to set it, any file with extension
.wav
will have itstype
set to one of these. If you want to check if it's a realaudio/wav
file, then check for its magic number :52 49 46 46
. (let r = new FileReader(); r.onload = e => console.log(new DataView(r.result).getUint32(0).toString(16) === '52494646'); r.readAsArrayBuffer(file.slice(0,8));}
) – Kaiido Commented Jul 20, 2017 at 2:57 -
1
And while I know the answer to your edit (simply change
console.log
toif
and addfile = new Blob([file], 'audio/wav')
after the closing)
in my first ment), I don't think it is correct to edit your question after someone (quite correctly) answered a previous version of it. – Kaiido Commented Jul 20, 2017 at 14:58 - 1 @Kaiido you're right and I think I'll accept this answer anyway. But there still was a misunderstanding (even without the edit) on this answer. So IMHO clarifying was mandatory. Anyway, his answer with your clarifying ments are both what I wanted so thank you! – Ulysse BN Commented Jul 20, 2017 at 15:01
-
2
ps: I had a typo :
file = new Blob([file], {type:'audio/wav'})
– Kaiido Commented Jul 20, 2017 at 15:04
1 Answer
Reset to default 13 +50Its important to understand that when you've stored a file on disk, the mime type is not stored within that type. Thats why we have file extensions like .jpg
or .wav
. In the internet over HTTP we don't need them. We can have an URL http://example./foo.wav
but send out a JPEG with the correct the JPEG mime type, and the browser will correcty render it as JPEG. It doesn't care about the file extension.
However if you are on your local file system the file extension is relevant. If you open a file called foo.wav
your operating system decides by the extension .wav
to open some audio player.
When selecting a file to upload it to the internet the bowser does a non-trivial task: It selects a mime type for the file extension. For this every browser has a mapping table, mapping known file extensions to mime types. Well, and here is the catch: this table obviously isn't identical on different browsers. So thats why you get different results in your fiddle.
Some browsers map .wav
to audio/wav
and some to audio/x-wav
.
So if your test case is downloading a file with the mime type audio.wav
and then inspecting its mime type with the fiddle you posted you don't check what mime type was sent by your server when you downloaded the file, but only what mime type is guessed for the file extension by your browser.
In both cases, if you sent out a file foo.wav
with the mime type audio/wav
or audio/x-wav
the file on your disk will be identical, and it won't be possible later to know what was the mime type your server sent for the file.
The only thing your browser can do during download is to change the file extension. For example if you sent a file http://example./foo
and it has audio/wav
as a mime type the browser will probably rename it to foo.wav
.