浏览器端:readAsText读取文本文件
使用readAsText方法读取文本文件
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText
// 接口调用
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方法读取文本文件
// 把服务器响应的文件下载到临时文件,根据需要读取文件的内容
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);
}
}
})