FileReader - which encodings are supported?

You just want to read the input / output files as text.

May rely on the use of modern browsers, so I use FileReader for this (which works like a charm).

reader.readAsText(myfile, encoding);

I know that encodingthe default is UTF-8.

But since my users will download files from different sources (Windows, Mac, Linux) and various browsers, I ask the user to provide the encoding through the selection box.

So, for example, for a Windows text file in the Western European format, I expect the user to select, for example. windows-1252.

I was unable to find a list of supported encodings for FileReader (assuming this at least depends on the browser).

I do not ask for automatic encoding detection, I just want to fill in the selection box as follows:

<select id="encoding">
   <option value="windows-1252">Windows (Western Latin)</option>
   <option value="utf-8">UTF-8</option>
   <option value="...">...</option>
</select>

So my questions are:

  • Where can I get a list of supported encodings to populate parameter values?
  • How to determine the exact record of these values ​​(is it "utf8" or "UTF-8" or ...) and depend on the OS / browser?
  • Does readAsText (myfile, unsupportedEncoding) produce any error that I can catch if the encoding is not supported?

I would prefer not to use any large third-party libraries for this.

Bonus question:

Is there an easy way to get meaningful translations of values, for example. Does cp10029 mean Mac (Central European)?

+9
source share
1 answer
  1. - https://github.com/whatwg/encoding/ ( JSON - https://github.com/whatwg/encoding/blob/master/encodings.json. "")

enter image description here

  1. .

  2. , readAsText (myfile, unsupportedEncoding) . ("utf8").

    window.onload = function() {

    //Check File API support
    if (window.File && window.FileList && window.FileReader) {
        var filesInput = document.getElementById("files");
    
        filesInput.addEventListener("change", function(event) {
    
            var files = event.target.files; //FileList object
            var output = document.getElementById("result");
    
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
    
                //Only plain text
                if (!file.type.match('plain')) continue;
    
                var picReader = new FileReader();
    
                picReader.addEventListener("load", function(event) {
    
                    var textFile = event.target;
    
                    var div = document.createElement("div");
    
                    div.innerText = textFile.result;
    
                    output.insertBefore(div, null);
    
                });
                //Read the text file
                picReader.readAsText(file, "cP1251");
            }
    
        });
    }
    else {
        console.log("Your browser does not support File API");
    }
    

    }

, JSON (https://github.com/whatwg/encoding/blob/master/encodings.json), "" "".

0

Source: https://habr.com/ru/post/1661868/


All Articles