Code Monkey home page Code Monkey logo

Comments (1)

ifdion avatar ifdion commented on May 25, 2024

@Mrbeyond did you solved it already?

Here is how I do it while using react-dropzone. Note that the component is a field input for formik.

import React, { useState } from 'react';
import Dropzone from 'react-dropzone';
import Resizer from 'react-image-file-resizer';

export default function Base64ImageField(props) {
  const [value, setValue] = useState(props.field.value);
  function handleDrop(acceptedFiles) {
    Resizer.imageFileResizer(
      acceptedFiles[0],
      100,
      100,
      'JPEG',
      100,
      0,
      uri => {
        setValue(uri);
        props.form.setFieldValue(props.field.name, uri);
      },
      'base64'
    );
  }

  return (
    <div className="">
      <Dropzone onDrop={handleDrop}>
        {({ getRootProps, getInputProps }) => (
          <div {...getRootProps({ className: 'tuv-uploadfile' })}>
            <input {...getInputProps()} />
            <p>
              <i className="fas fa-cloud-upload-alt"></i> Drag'n'drop a files
              here, or click it!
            </p>
          </div>
        )}
      </Dropzone>
      {value && <img src={value} alt={props.field.name} />}
    </div>
  );
}

from react-image-file-resizer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.