This commit is contained in:
从何开始123
2026-01-09 01:07:03 +08:00
parent 21842c2b50
commit a32f3a5faf
17 changed files with 334 additions and 135 deletions

View File

@@ -20,4 +20,21 @@ export const cleanJsonString = (str: string) => {
// 3. Fallback: return original if it looks like JSON, otherwise empty object
return str.trim().startsWith('{') ? str : "{}";
};
export const fileToBase64 = (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
if (typeof reader.result === 'string') {
// Remove the Data URL prefix (e.g., "data:image/png;base64,")
const base64 = reader.result.split(',')[1];
resolve(base64);
} else {
reject(new Error('Failed to convert file to base64'));
}
};
reader.onerror = error => reject(error);
});
};