前端知识(十一)———js判断上传的文件是GBK编码还是UTF-8

1、获取文件二进制数据,这里只做示例,例如element-ui中文件上传的beforeUpload方法,返回的file对象,然后使用FileReader对其进行转换,再进行后续判断

javascript 复制代码
function beforeUpload(file: File) { 
    const reader = new FileReader();
 
    reader.readAsArrayBuffer(file);
 
    reader.onload = function (e: any) {
       // 等待file文件对象转换完成
    }
}

2、将二进制数据处理为无符号整数,也就是处理为字节

javascript 复制代码
reader.onload = function (e: any) {
     const btyes= new Uint8Array(e.target.result)
     console.log(btyes);
     if (isUTF8(btyes)) {
       console.log('utf-8');
     } else {
       console.log('GBK');
     }
}

3、isUTF8函数

javascript 复制代码
// 判断文件编码格式的函数
function isUTF8(bytes) {
  var i = 0;
  while (i < bytes.length) {
    if ((// ASCII
      bytes[i] == 0x09 ||
      bytes[i] == 0x0A ||
      bytes[i] == 0x0D ||
      (0x20 <= bytes[i] && bytes[i] <= 0x7E)
    )
    ) {
      i += 1;
      continue;
    }
 
    if ((// non-overlong 2-byte
      (0xC2 <= bytes[i] && bytes[i] <= 0xDF) &&
      (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF)
    )
    ) {
      i += 2;
      continue;
    }
 
    if ((// excluding overlongs
      bytes[i] == 0xE0 &&
      (0xA0 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
      (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
    ) ||
      (// straight 3-byte
        ((0xE1 <= bytes[i] && bytes[i] <= 0xEC) ||
          bytes[i] == 0xEE ||
          bytes[i] == 0xEF) &&
        (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
        (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
      ) ||
      (// excluding surrogates
        bytes[i] == 0xED &&
        (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x9F) &&
        (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
      )
    ) {
      i += 3;
      continue;
    }
 
    if ((// planes 1-3
      bytes[i] == 0xF0 &&
      (0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
      (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
      (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
    ) ||
      (// planes 4-15
        (0xF1 <= bytes[i] && bytes[i] <= 0xF3) &&
        (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
        (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
        (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
      ) ||
      (// plane 16
        bytes[i] == 0xF4 &&
        (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8F) &&
        (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
        (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
      )
    ) {
      i += 4;
      continue;
    }
    return false;
  }
  return true;
}
相关推荐
这个需求建议不做2 分钟前
vue3打包配置 vite、router、nginx配置
前端·nginx·vue
翔云API4 分钟前
身份证识别接口的应用场景和作用
运维·服务器·开发语言·自动化·ocr
QGC二次开发7 分钟前
Vue3 : Pinia的性质与作用
前端·javascript·vue.js·typescript·前端框架·vue
学java的小菜鸟啊18 分钟前
第五章 网络编程 TCP/UDP/Socket
java·开发语言·网络·数据结构·网络协议·tcp/ip·udp
云草桑18 分钟前
逆向工程 反编译 C# net core
前端·c#·反编译·逆向工程
立黄昏粥可温22 分钟前
Python 从入门到实战22(类的定义、使用)
开发语言·python
布丁椰奶冻23 分钟前
解决使用nvm管理node版本时提示npm下载失败的问题
前端·npm·node.js
PerfMan25 分钟前
基于eBPF的procstat软件追踪程序垃圾回收(GC)事件
linux·开发语言·gc·ebpf·垃圾回收·procstat
聆听HJ33 分钟前
java 解析excel
java·开发语言·excel
溪午闻璐36 分钟前
C++ 文件操作
开发语言·c++