mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-29 17:55:24 +00:00
V4.6.6-1 (#656)
This commit is contained in:
53
packages/web/common/file/read.ts
Normal file
53
packages/web/common/file/read.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { uploadMarkdownBase64 } from '@fastgpt/global/common/string/markdown';
|
||||
import { htmlStr2Md } from '../string/markdown';
|
||||
/**
|
||||
* read file raw text
|
||||
*/
|
||||
export const readFileRawText = (file: File) => {
|
||||
return new Promise((resolve: (_: string) => void, reject) => {
|
||||
try {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
resolve(reader.result as string);
|
||||
};
|
||||
reader.onerror = (err) => {
|
||||
console.log('error txt read:', err);
|
||||
reject('Read file error');
|
||||
};
|
||||
reader.readAsText(file);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const readMdFile = async ({
|
||||
file,
|
||||
uploadImgController
|
||||
}: {
|
||||
file: File;
|
||||
uploadImgController: (base64: string) => Promise<string>;
|
||||
}) => {
|
||||
const md = await readFileRawText(file);
|
||||
const rawText = await uploadMarkdownBase64({
|
||||
rawText: md,
|
||||
uploadImgController
|
||||
});
|
||||
return rawText;
|
||||
};
|
||||
|
||||
export const readHtmlFile = async ({
|
||||
file,
|
||||
uploadImgController
|
||||
}: {
|
||||
file: File;
|
||||
uploadImgController: (base64: string) => Promise<string>;
|
||||
}) => {
|
||||
const md = htmlStr2Md(await readFileRawText(file));
|
||||
const rawText = await uploadMarkdownBase64({
|
||||
rawText: md,
|
||||
uploadImgController
|
||||
});
|
||||
|
||||
return rawText;
|
||||
};
|
Reference in New Issue
Block a user