question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dropping Image not setting files to input.

See original GitHub issue
<Dropzone onDrop={this.onDrop} className="drop-zone text-center" name="building_photo_1"
                        inputProps={{size: '30', required: "true"}}>
             <i className="fa fa-upload fa-4x"/>
             <div className="drop-zone-text">
                   {!this.state.fileNames ?<div>Select files to upload<p>or drag and drop photo files</p></div>
                   : <div>{this.state.fileNames}</div>}
             </div>
</Dropzone>

Selecting files sets the files to the input perfectly, but when dropping the file the files are not being set to the input.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:19

github_iconTop GitHub Comments

4reactions
ihgrantcommented, Dec 5, 2016

As far as I can tell, it’s not possible to set the files property on a file input using javascript, so this is not a bug in react-dropzone.

The way I ended up working around this issue is by submitting the form using fetch, and storing the files that were dropped in state and adding them to a FormData instance created from the form, e.g.:

    onDrop(acceptedFiles, rejectedFiles, e) {
        this.setState({
            files: acceptedFiles,
            filesWereDropped: !e.target.files || e.target.files.length === 0
        });
    }
    onSubmit(e) {
        e.preventDefault();
        let formData = new FormData(this.formRef);

        if (this.state.filesWereDropped) {
            /* if the file input has no files, check state for files and add
             * those to the form data. This is necessary because dragging
             * and dropping does set the files property of the file input,
             * and it is not possible to set it from javascript for
             * security reasons.
             */
            this.state.files.forEach(file => {
                formData.append('myfiles', file, file.name);
            });
        }

        // then POST your formData!
    }
1reaction
ihgrantcommented, Aug 29, 2018

@micchyboy237, not possible, as I explained above. It’s a limitation put in place by browsers for security.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set file input value when dropping file on page?
The user drops the file in the preview-image-container . The file isn't submitted with AJAX, the user needs to submit the form which...
Read more >
How To Make A Drag-and-Drop File Uploader With Vanilla ...
It's a known fact that file selection inputs are difficult to style the ... Drag-and-drop image uploader in action ... Setting Up Our...
Read more >
Drag and Drop File Uploading - CSS-Tricks
No problems at all if drag & drop file upload is not supported. ... (IE and Firefox) do not allow setting the value...
Read more >
Creating a ReactJS drag and drop file upload component
But we don't want our users to see that file input, because we don't live in the olden days. And we have no...
Read more >
react-dropzone
Simple React hook to create a HTML5-compliant drag'n'drop zone for files. ... the props from getInputProps() , opening a file dialog will not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found