kstulgysK
Convex Community2y ago
7 replies
kstulgys

Is this is legit way to upload files? Haven't seen this in any example

With this approach you do not have to create http route.

frontend:
function convertFileToBase64(file: File): Promise<string> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onloadend = () => resolve(reader.result as string);
    reader.onerror = reject;
    reader.readAsDataURL(file);
  });
}

  const upload = useAction(api.image.upload);

  const handleUploadLogo = async (file: File | null) => {
    if (!file) return;
    const base64 = await convertFileToBase64(file);
    await upload({ base64, table: "accounts", column: "logo" });
  };
Was this page helpful?