如何js讀取utf-16le編碼文件的數(shù)據(jù)信息

瀏覽器端:readAsText讀取文本文件

使用readAsText方法讀取文本文件
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText

// 接口調(diào)用
import { Injectable } from "@angular/core";

@Injectable({
    providedIn: 'root'
  })
  export class BusinessSharedService {
    constructor() { }
    //獲取文件blob
    getImgArrayBuffer(url: string) {
        return new Promise((resolve, reject) => {
           var blob = null;
           let imgFile = null;
           var xhr = new XMLHttpRequest();
           xhr.open("GET", url, true);
           xhr.responseType = "blob";
           xhr.onload = () => {
              blob = xhr.response;
               imgFile = new File([blob], "imageName", { type: "image/jpeg" });
               resolve(imgFile);
           };
    
          xhr.onerror = e => {
            reject(e);
          };
          xhr.send();
      })
    }
  } 
  this.businessSharedService.getImgArrayBuffer(`${url}${this.id}`).then((item: any) => {
      //  utf-16le編碼文件的信息
      var reader = new FileReader();
      reader.readAsText(item, 'UTF-16LE')
        reader.onload = function (e: any) {
          const val: any = e.target.result;
          console.log('獲取utf-16le編碼文件的信息', val)
      }
  })

小程序端使用readAsText方法讀取文本文件

     // 把服務(wù)器響應(yīng)的文件下載到臨時(shí)文件,根據(jù)需要讀取文件的內(nèi)容
    wx.downloadFile({
        url: `${url}${id}`, // 下載ota.hex文件
        success(res) {
           console.log(res)
            if (res.statusCode === 200) {
                 //讀取文件信息
                let arrBuffer: any = fs.readFileSync(res.tempFilePath, 'utf-16le');
                const fileMessage = JSON.parse(arrBuffer);
                console.log("讀取文件信息:", fileMessage);
            }
         }
    })
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言: 之前我總以為瀏覽器上傳文件就一種方式——表單(表單包括 HTML 的 Form 表單,和虛擬表單 Form...
    CondorHero閱讀 2,998評(píng)論 0 2
  • 最近在做的一個(gè)項(xiàng)目,需要讀取用戶上傳的文件并進(jìn)行本地存儲(chǔ),學(xué)習(xí)了一下FileReader。本文簡(jiǎn)單介紹了FileR...
    杜伊特閱讀 25,741評(píng)論 2 8
  • 筆者之前遇了好幾處需要處理文件流的場(chǎng)景,以及互相轉(zhuǎn)換,這邊深入的學(xué)習(xí)了一下它們的概念,區(qū)別以及互相轉(zhuǎn)換,閱讀本文你...
    NinthWorld94閱讀 3,192評(píng)論 0 1
  • FileReader主要用于將文件內(nèi)容讀入內(nèi)存,通過一系列異步接口,可以在主線程中訪問本地文件。可以使用File對(duì)...
    江疏影子閱讀 482評(píng)論 1 0
  • 一般情況下,想要實(shí)現(xiàn)文件下載/導(dǎo)出功能,需要在前端把數(shù)據(jù)發(fā)到服務(wù)端或者發(fā)送下載請(qǐng)求到服務(wù)端,然后由服務(wù)端通過獲取數(shù)...
    xlaoyu閱讀 5,850評(píng)論 2 50

友情鏈接更多精彩內(nèi)容