0

This is the input tag that I am using to select a file,

<input id="imprtUpldFileInpt"  name="file" type="file"/>

And this is how I process the file details with jQuery,

var file = $("#imprtUpldFileInpt")[0].files[0];
var f_name = file.name;
var f_size = file.size;
var f_type = file.type;
var fileMimeTypeArr = ["text/comma-separated-values", "text/csv", "application/csv", "application/vnd.ms-excel"];
var fileTypePos = $.inArray(f_type, fileMimeTypeArr);

I am checking whether the selected file is a csv or not with the value of fileTypePos. If the value is greater than or equal to 0, then I conform that it is a csv file. But I am facing a problem in getting the file type.

If a csv is selected, the file name and size are obtained correctly. But I am not getting the file type.

The following screenshot shows the file details of a csv file, enter image description here

I have added the possible mime types in fileMimeTypeArr for checking whether the file is a csv file or not. The problem is with getting the file type.

This problem does not occur if the csv file is saved in a machine that has MS Excel installed. But if a csv file is created/saved in a machine that does not have MS Excel, then this problem occurs.

Why the file type has ""?

  • Possible duplicate of [What MIME type should I use for CSV?](http://stackoverflow.com/questions/7076042/what-mime-type-should-i-use-for-csv) – online Thomas Jul 04 '16 at 14:47
  • That link tells the mime type that should be used for csv files. But my problem is getting the mime type. – Deepika Masilamani Jul 04 '16 at 14:50
  • I tried it using `var file = $("#imprtUpldFileInpt").files[0];` and i got the `type : "application/vnd.ms-excel"` when uploading a csv file. – bhansa Jul 04 '16 at 14:58
  • @Bhansa That is because, you have saved the file in MS Excel. Try saving it is a normal csv file, then you will not get the type. Sorry for not mentioning this in my post. – Deepika Masilamani Jul 04 '16 at 14:59
  • no, I have saved it in csv format. – bhansa Jul 04 '16 at 15:04
  • *"[position] greater than 0"* - You need to test greater than or equal to zero. Anyway, a CSV file could be created by simply opening Notepad and typing/pasting in some data, or any number of other methods where the program that created the file didn't set a CSV-related MIME type. – nnnnnn Jul 04 '16 at 15:06
  • @Bhansa Have you created the csv in notepad and saved it with .csv extension? – Deepika Masilamani Jul 04 '16 at 15:06
  • yes exactly @DeepikaMasilamani. – bhansa Jul 04 '16 at 15:08
  • @nnnnnn Thanks for pointing out the typo. But how can I handle the type if the mime type is not set? – Deepika Masilamani Jul 04 '16 at 15:08
  • 1
    @Bhansa Then why for me it is not working. In one of my VMs I have MS Excel installed. If I upload a file from that VM, then it is working. But if I try to upload a file from a machine that does not have MS Excel, then it is not working. – Deepika Masilamani Jul 04 '16 at 15:10
  • try this and tell me the output: https://jsfiddle.net/Lp21neh6/1/ – bhansa Jul 04 '16 at 15:55
  • @Bhansa It is not showing anything. Empty alert box! – Deepika Masilamani Jul 05 '16 at 06:01

0 Answers0