js如何判断文件的字符集编码是什么格式呢?

如题:
js如何判断文件的字符集编码是什么格式呢?
我有一个需求是读取文本。并显示文本。目前是导入txt格式的。只能把那个编码格式写死。怎样才能去获取选中文本的编码格式呢?

    //读取文件
    readFile() {
      let fileSelect = document.querySelector('input[type=file]').files[0]//找到文件上传的元素
      let reader = new FileReader()
      if (typeof FileReader === 'undefined') {
        console.log('您的浏览器不支持FileReader接口')
        return
      }
      // reader.readAsText(fileSelect, 'gb2312')//注意读取中文的是用这个编码,不是utf-8
      reader.readAsText(fileSelect, 'utf-8')//注意读取中文的是用这个编码,不是utf-8
      const _this = this
      reader.onload = function() {
        // console.log(reader.result)
        _this.$nextTick(() => {
          _this.query.voiceContent = reader.result
          // console.log(_this.query.voiceContent)
        })
      }
      console.log(reader)

    },

image.png

阅读 5k
1 个回答

可以尝试关键字“文件前三个字节”搜索一下,因为没有实践过,可以自行感受一下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题