Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting src using base64 data doesn't render the image #74

Open
anshul-kai opened this issue Aug 31, 2024 · 0 comments
Open

Setting src using base64 data doesn't render the image #74

anshul-kai opened this issue Aug 31, 2024 · 0 comments

Comments

@anshul-kai
Copy link

The snippet below doesn't render a base64 image. Using the same snippet with file select works just fine. Am I doing something wrong here?

<Cropper
  ref={cropperRef}
  src={image?.src ?? base64Img}
  onUpdate={_onUpdate}
  stencilProps={{
    aspectRatio: 1
  }}
  className="cropper"
/>

const _onUpdate = useCallback((cropper: CropperRef) => {
  previewRef.current?.update(cropper);
}, [base64Img, previewRef]);

const _onFileSelect = useCallback((e: ChangeEvent) => {
  const { files } = e.target as HTMLInputElement;
  if (files && files.length) {
    const blob = URL.createObjectURL(files[0]);

    // Get the image type from the extension. It's the simplest way, though be careful it can lead to an incorrect result:
    setImage({
      src: blob,
      type: files[0].type,
      ext: files[0].name.split('.').pop()?.toLowerCase()
    });
  }

  // Clear the event target value to give the possibility to upload the same image:
  (e.target as HTMLInputElement).value = '';
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant