Can we convert ANSI encoded CSV file to utf-8 encoded file with javascript?

6.2k views Asked by At

I have been looking answer for this question for days. Actually, I need to upload the ansi encoded csv file to web service. The front end application reads the file encode it's content to base64 and send it to web service in json format. But when I try to read file with javascript, non-english unicodes just gets changed to something else.

Is there any way to read ansi encoded csv file in javascript and converting its encoding to utf-8?

With my research, I think is impossible. The file must be in UTF-8 itself.

Any suggestions would be great help.

1

There are 1 answers

5
Kirill Slatin On BEST ANSWER

I am sorry I misread you question at first. Your trouble comes at the moment of reading the file. It is useless to try to convert the file after contents were ruined when trying to load in a wrong encoding. I came up with FileReader API in this fiddle inspired by this example and article on html5rocks

  var r = new FileReader();
  r.readAsText(f, 'windows-1252');

The only trouble I see here is there is no auto encoding detection. You need to know encoding before loading the file.