0

I am not sure how can I find the encoding of the file. I am trying to upload the CSV file only with utf-8 and find if there are any non utf-8 characters I want to show an error message.

I am using Papa persor for parsing.

How can I read the encoding of the file either in java or in js.

var fileElement = element.find('.csv-upload');
var file = fileElement[0].files[0]
var parseConfig = {
        skipEmptyLines: true,
        header: true,
        encoding:'UTF-8',
        trimHeaders: true,
        complete: function (content) {
          scope.onFileRead(content, fileElement[0]);
        }
      };

      if (scope.rowsToParse) {
        parseConfig.preview = scope.rowsToParse;
      }

      if (file) {
        Papa.parse(file, parseConfig);
      }
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Seeta Ram Yadav
  • 847
  • 9
  • 16

1 Answers1

0

A CSV file will not have any encoding information in it. It is just a sequence of bytes.

You have to know beforehand whether the file contains non-UTF8 characters, and what is the right file encoding to use when reading it.

If you are using Java (not sure if you are because you have the Spring tag), you could use one of the libraries suggested here to try and infer the file type.

Is there a java library equivalent to file command in unix

Maybe there is something similar for Javascript.

jbx
  • 21,365
  • 18
  • 90
  • 144
  • Thank you for trying to answer my question. I think there is encoding even in CSV file. to check please we can use ``file -I "filename"`` – Seeta Ram Yadav Nov 01 '18 at 10:28
  • No there isn't. The unix `file` command just tries to infer it from various hints, including analysing its content for special characters. https://www.computerhope.com/unix/ufile.htm Maybe you can see if there is a library that does the same thing. – jbx Nov 01 '18 at 10:33