前一阵子,後端有个需求是在档案上传前,需要提前知道上传的档案编码 是 UTF-8 或是 ascii 等,才能决定要不要将档案放进资料库,或是在这之前再做一次转档。
今天的内容跟 Angular 比较没有关系,属於套件分享,但因也是使用 Angular 实作,所以也就放进来了。
npm i jschardet
template
<div class="form-group">
<label for="file">Choose File</label>
<input type="file" id="file" (change)="fileChange($event)" />
</div>
<br />
Your Encoding:{{ encoding$ | async }}
.ts 档
import { Component, VERSION } from "@angular/core";
import jschardet from "jschardet";
import { Subject } from "rxjs";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent {
fileToUpload: File = null;
encoding$ = new Subject();
fileChange(event: any): void {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e: any) => {
const csvResult = e.target.result.split(/\r|\n|\r\n/);
const encoding = jschardet.detect(csvResult.toString()).encoding;
this.encoding$.next(encoding);
};
reader.readAsBinaryString(file);
}
}
范例:
https://stackblitz.com/edit/angular-detect-encoding
参考资料:
csv-encoding-detection-javascript
後来使用 node 写成小工具
git:https://github.com/mtwmt/detect-encoding
>>: Day16 Vue directives(v-model资料双向绑定)
一般在使用资料库新增资料的时候,都会看到新建资料的日期跟时间,今天会再sqllite上加入日期。 我...
先简单回顾一下,今天预计分析的题目: 题目连结:https://leetcode.com/prob...
本篇要分享的是此书(在第三章)我蛮喜欢的范例之一,作者以说故事的方式讲解本章节所介绍的题目,假设的情...
该文章同步发布於:我的部落格 昨天的文章介绍了 TDD 的流程和精神等等。 今天我们要正式进入 R...
Adapter 一笔资讯的内容称为项目(Item),而负责将资料转换成资讯的就是Adapter,Ad...