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');
});
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 } }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With