7

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');
});

2 Answers 2

3

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 } }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I know this is an old post, but since it is unanswered, I'd like to throw this out there to anyone who might be interested:

You should check out this library encoding.js

They also have a working demo. I would suggest you first try it out with the files that you'll typically work with to see if it detects the encoding correctly and then use the library in your project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.