Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Read Mime Type CSV Always Empty

I'm currently struggling with Windows Browser to check mimetype of a csv file. I've tried checking it via:

console.log($('#file')[0].files[0].type); // output: ""

But when I use Linux browser, it works fine:

console.log($('#file')[0].files[0].type); // output: "text/csv"

How could this happen? Is there any alternative solution for this? Any help would be appreciated.

Here's my Windows + browser spec:

  • Chromium 50.0.2632.0
  • Windows 10 Pro 64-bit

Update 1

When I tried to upload a jpg / png / any image file, Chromium can detect it's mime-type.

I'm sorry, English is not my native.

like image 223
krisanalfa Avatar asked May 26 '16 16:05

krisanalfa


2 Answers

The mime-type is set by the browser depending on what it extensions it "knows" about. Internet Explorer doesn't have the mime-type for .csv see this list here. I would just check for .csv file extension manually on IE. Or you could use an external library that knows more than IE does.

Ultimately what gets placed in file.type is set by the browser. You will need to check the extension manually with either a library (I couldn't find one), or a switch statement such as this suggestion.

You can look up a list of mime-types and just add the ones you think you will encounter.

EDIT

The browser also supplies the FileReader object. You can use that to check things client-side. With Javascript being single threaded, I have no idea how well it would handle large files though. Here is a link.

like image 178
RayfenWindspear Avatar answered Oct 28 '22 12:10

RayfenWindspear


The main problem is - if you have Microsoft office, then your browsers will work just fine and will see csv type, if you don't have it - it won't

like image 40
Виталий Сдельников Avatar answered Oct 28 '22 14:10

Виталий Сдельников