sujjeee / shadcn-image-cropper

A minimal image cropper UI built with shadcn and the react-image-crop library!
https://shadcn-image-cropper.vercel.app
111 stars 3 forks source link

How to handle uploadfile in server action #5

Open dzakyabdurhmn opened 1 week ago

dzakyabdurhmn commented 1 week ago

how to handle input with default value or defaultvalue from cropped file like this

      <form action={uploadFiles}>
        <input value={cropped} type="file" name="files" multiple />
        <button type="submit">Upload Files</button>
      </form>
sujjeee commented 1 week ago

@dzakyabdurhmn what are you tring to do actually with input ?

dzakyabdurhmn commented 1 week ago

@sujjeee want to upload the image to storage/bucket like aws

sujjeee commented 1 week ago

@dzakyabdurhmn im doing like this

  async function uploadAvatar() {
    try {
      setIsUploading(true)

      const formData = new FormData()

      if (croppedImageUrl) {
        const croppedImageBlob = await base64ToBlob(croppedImageUrl)

        // Check the size of the blob
        const maxSize = 5 * 1024 * 1024 // 5MB
        if (croppedImageBlob.size > maxSize) {
          throw new Error("Image size exceeds the maximum limit of 5MB")
        }

        formData.append("file", croppedImageBlob)
      }

      const { error } = await updateUserAvatar({
        formData,
        userEmail: user.email,
        userId: user.id,
        workspacePermissions: permissions,
      })

      if (error) throw new Error(error)

      toast("User avatar updated")
    } catch (error) {
      showErrorToast(error)
    } finally {
      setIsUploading(false)
    }
  }