mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
Fix garbled code (#3126)
* 修复:修复引导输入批量导入中文乱码问题 * 兼容非UTF-8的CSV导入 --------- Co-authored-by: 勤劳上班的卑微小张 <jiazhan.zhang@ggimage.com>
This commit is contained in:
@@ -61,7 +61,14 @@ export const readFileRawText = ({
|
|||||||
|
|
||||||
reject(getErrText(err, 'Load file error'));
|
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) {
|
} catch (error) {
|
||||||
reject('The browser does not support file content reading');
|
reject('The browser does not support file content reading');
|
||||||
}
|
}
|
||||||
@@ -71,6 +78,29 @@ export const readFileRawText = ({
|
|||||||
export const readCsvRawText = async ({ file }: { file: File }) => {
|
export const readCsvRawText = async ({ file }: { file: File }) => {
|
||||||
const rawText = await readFileRawText({ file });
|
const rawText = await readFileRawText({ file });
|
||||||
const csvArr = Papa.parse(rawText).data as string[][];
|
const csvArr = Papa.parse(rawText).data as string[][];
|
||||||
|
|
||||||
return csvArr;
|
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';
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user