✨ complete mobile image upload
This commit is contained in:
@@ -6,8 +6,7 @@ export const localforageStorageAdapter = {
|
||||
await localforage.setItem(key, value);
|
||||
},
|
||||
async get(key: string) {
|
||||
const res: any = await localforage.getItem(key);
|
||||
return res ? JSON.parse(res) : null;
|
||||
return await localforage.getItem(key);
|
||||
},
|
||||
async remove(key: any) {
|
||||
await localforage.removeItem(key);
|
||||
|
22
src/utils/imageUtils/blobToBase64.ts
Normal file
22
src/utils/imageUtils/blobToBase64.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export async function blobToBase64(blobUrl: string): Promise<string> {
|
||||
try {
|
||||
const response = await fetch(blobUrl);
|
||||
const blob = await response.blob();
|
||||
const reader = new FileReader();
|
||||
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
reader.onload = function () {
|
||||
// 直接使用 reader.result,包含 MIME 类型前缀
|
||||
const base64StringWithPrefix = reader.result!.toString();
|
||||
resolve(base64StringWithPrefix);
|
||||
};
|
||||
reader.onerror = function () {
|
||||
reject("File could not be read");
|
||||
};
|
||||
// 读取 Blob 文件到 Data URL 格式
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error("Error fetching blob from URL: " + error);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user