Skip to content

Commit

Permalink
Fix garbled code (#3126)
Browse files Browse the repository at this point in the history
* 修复:修复引导输入批量导入中文乱码问题

* 兼容非UTF-8的CSV导入

---------

Co-authored-by: 勤劳上班的卑微小张 <[email protected]>
  • Loading branch information
2277419213 and 勤劳上班的卑微小张 authored Nov 13, 2024
1 parent c12159b commit 98d4a6e
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions packages/web/common/file/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ export const readFileRawText = ({

reject(getErrText(err, 'Load file error'));
};
reader.readAsText(file);
detectFileEncoding(file).then((encoding) => {
console.log(encoding);

reader.readAsText(
file,
['iso-8859-1', 'windows-1252'].includes(encoding) ? 'gb2312' : 'utf-8'
);
});
} catch (error) {
reject('The browser does not support file content reading');
}
Expand All @@ -71,6 +78,29 @@ export const readFileRawText = ({
export const readCsvRawText = async ({ file }: { file: File }) => {
const rawText = await readFileRawText({ file });
const csvArr = Papa.parse(rawText).data as string[][];

return csvArr;
};

interface EncodingDetectionResult {
encoding: string | null;
}

function detectEncoding(buffer: ArrayBuffer): EncodingDetectionResult {
const encodings = ['utf-8', 'iso-8859-1', 'windows-1252'];
for (let encoding of encodings) {
try {
const decoder = new TextDecoder(encoding, { fatal: true });
decoder.decode(buffer);
return { encoding }; // 如果解码成功,返回当前编码
} catch (e) {
// continue to try next encoding
}
}
return { encoding: null }; // 如果没有编码匹配,返回null
}

async function detectFileEncoding(file: File): Promise<string> {
const buffer = await loadFile2Buffer({ file });
const { encoding } = detectEncoding(buffer);
return encoding || 'unknown';
}

0 comments on commit 98d4a6e

Please sign in to comment.