1- import React , { Component , Fragment } from 'react' ;
2- import PropTypes from 'prop-types' ;
1+ import classnames from 'classnames' ;
32import { observer } from 'mobx-react' ;
43import { Field } from 'mobx-react-form' ;
5- import classnames from 'classnames' ;
4+ import PropTypes from 'prop-types' ;
5+ import { Component , Fragment } from 'react' ;
66import Dropzone from 'react-dropzone' ;
77
88export default @observer class ImageUpload extends Component {
@@ -21,17 +21,40 @@ export default @observer class ImageUpload extends Component {
2121
2222 state = {
2323 path : null ,
24+ image : null ,
2425 }
2526
2627 dropzoneRef = null ;
2728
2829 onDrop ( acceptedFiles ) {
2930 const { field } = this . props ;
3031
32+ console . log ( 'acceptedFiles' , acceptedFiles ) ;
33+
3134 acceptedFiles . forEach ( ( file ) => {
32- this . setState ( {
33- path : file . path ,
34- } ) ;
35+ if ( file ) {
36+ const reader = new FileReader ( ) ;
37+
38+ // When the file reader is done, this function runs
39+ reader . onloadend = ( ) => {
40+ // `reader.result` is something like: "data:image/png;base64,iVBORw0K..."
41+ const base64Data = reader . result ;
42+
43+ // If you need just the base64 portion without the data URL prefix:
44+ // const base64String = base64Data.split(',')[1];
45+
46+ console . log ( 'Base64 Image:' , base64Data ) ;
47+
48+ this . setState ( {
49+ image : base64Data ,
50+ } ) ;
51+ // Now you can use `base64Data` (or `base64String`) as needed.
52+ } ;
53+
54+ // Read the file as a Data URL
55+ reader . readAsDataURL ( file ) ;
56+ }
57+
3558 this . props . field . onDrop ( file ) ;
3659 } ) ;
3760
@@ -52,16 +75,18 @@ export default @observer class ImageUpload extends Component {
5275 [ `${ className } ` ] : className ,
5376 } ) ;
5477
78+ console . log ( 'state' , this . state ) ;
79+
5580 return (
5681 < div className = "image-upload-wrapper" >
5782 < label className = "franz-form__label" htmlFor = "iconUpload" > { field . label } </ label >
5883 < div className = "image-upload" >
59- { ( field . value && field . value !== 'delete' ) || this . state . path ? (
84+ { ( field . value && field . value !== 'delete' ) || this . state . image ? (
6085 < Fragment >
6186 < div
6287 className = "image-upload__preview"
6388 style = { ( {
64- backgroundImage : `url("${ this . state . path || field . value } ")` ,
89+ backgroundImage : `url("${ this . state . image || field . value } ")` ,
6590 } ) }
6691 />
6792 < div className = "image-upload__action" >
@@ -72,7 +97,7 @@ export default @observer class ImageUpload extends Component {
7297 field . set ( 'delete' ) ;
7398 } else {
7499 this . setState ( {
75- path : null ,
100+ image : null ,
76101 } ) ;
77102 }
78103 } }
0 commit comments