1

I want to upload images in reactjs I am using Formik following https://codesandbox.io/s/lkkjpr5r7?file=/index.js:1808-1812

my code is:

               <Field
                className="attribute_input"
                name={this.props.fieldname}
                type="file"
                placeholder={
                    this.props.title
                }
                onChange={this.handleChange}
                   
                />

and handlechange has the folloing implementations:

handleChange(event) {
        const reader = new FileReader();
        reader.onload = () => {
            if (reader.readyState === 2) {
                this.setState({file: reader.result})
            }
        } 
        reader.readAsDataURL(event.target.files[0]);
        console.log(this.props.fieldname);
        this.props.sfv("image1", event.currentTarget.files[0]);
    }

Facing error:

Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.

1 Answer 1

1

The issue was <Field tag. Formik did not provide native file tag so we need to use <input tag instead of its basic field tag. In demo they are also using this but I missed this thing. Tag should be as following:

        <input
        className="attribute_input"
        name={this.props.fieldname}
        type="file"
        onChange={this.handleChange}
        />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.