js FileReader读取的文件内容怎么改变编码格式?


我用FileReader API的readAsText读取上传的txt文件内容,但是txt的编码格式是gb2312,在网页上显示要utf-8才可以,怎么能转换一下呢?求助

web前端开发 api JavaScript

诚实的狐狸 10 years, 2 months ago

 var fs = require('fs');

fs.readFile("test.txt", {encoding: 'utf8'}, function(err, data ) {
   console.log( data );
});

opera85 answered 10 years, 2 months ago


 var reader = new FileReader();

reader.onload = function(e) {
  var text = reader.result;
}

reader.readAsText(file, encoding);

文档: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText

月光琉璃色 answered 10 years, 2 months ago

Your Answer