Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect encoding of html FileReader?

txt file maybe utf8/GB2312,.... but if upload to my server, i got ascii only. how to detect file encoding, so i can set in readAsText()?

$("#fileinput").change(function(evt){
  if (!checkSupport())return; 
  var f = evt.target.files[0]; 
  if (!f) return;
  var r = new FileReader();
  r.onload = function(evt){   //file loaded successfuly
    g_fname=f.name;
    g_contents = evt.target.result;
    curpage.val(0);
    read_article();
  }
  r.readAsText(f,'GB2312');
});
like image 227
whi Avatar asked Jun 18 '14 14:06

whi


1 Answers

As of 2021 I think the easiest solution is to use detect-file-encoding-and-language!

You can simply load it via the <script> tag:

  <script src="https://unpkg.com/detect-file-encoding-and-language/umd/language-encoding.min.js"></script>

From the documentation:

// index.html
<body>
  <input type="file" id="my-input-field" />
  <script src="https://unpkg.com/detect-file-encoding-and-language/umd/language-encoding.min.js"></script>
  <script src="app.js"></script>
</body>
// app.js
document.getElementById("my-input-field").addEventListener("change", inputHandler);
function inputHandler(e) {
    const file = e.target.files[0];
    languageEncoding(file).then(fileInfo => console.log(fileInfo));
    // Possible result: { language: english, encoding: UTF-8, confidence: { language: 0.97, encoding: 1 } }
}
like image 189
Falaen Avatar answered Sep 30 '22 19:09

Falaen