-
-
Notifications
You must be signed in to change notification settings - Fork 780
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
No files in input after drag&drop files when submitting form #880
Comments
It seems that the drag'n drop doesn't set the new files to the input. |
@FredoVelcro next time you open an issue, please take the time to create a small codepen/codesandbox to illustrate your issue. And yes, we do not update the input when you drag 'n' drop a file. You have the Otherwise you're welcome to make a proposal for a different behavior and implement in a PR. |
thank you for your help |
If anyone needs it, you can send the files along with form submissions, by munging the files into a hidden file input. import React, {useRef} from 'react';
import {useDropzone} from 'react-dropzone';
function Dropzone(props) {
const {required, name} = props;
const hiddenInputRef = useRef(null);
const {getRootProps, getInputProps, open, acceptedFiles} = useDropzone({
// Disable click and keydown behavior
noClick: true,
noKeyboard: true,
onDrop: (incomingFiles) => {
console.log(hiddenInputRef);
if (hiddenInputRef.current) {
// Note the specific way we need to munge the file into the hidden input
// https://stackoverflow.com/a/68182158/1068446
const dataTransfer = new DataTransfer();
incomingFiles.forEach((v) => {
dataTransfer.items.add(v);
});
hiddenInputRef.current.files = dataTransfer.files;
}
}
});
const files = acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<div className="container">
<div {...getRootProps({className: 'dropzone'})}>
{/*
Add a hidden file input
Best to use opacity 0, so that the required validation message will appear on form submission
*/}
<input type ="file" name={name} required={required} style ={{opacity: 0}} ref={hiddenInputRef}/>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here</p>
<button type="button" onClick={open}>
Open File Dialog
</button>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</div>
);
}
<form onSubmit={(e) => {
e.preventDefault();
// Now get the form data as you regularly would
const formData = new FormData(e.currentTarget);
const file = formData.get("my-file");
alert(file.name);
}}>
<Dropzone name ="my-file" required/>
<button type="submit">Submit</button>
</form> |
I use a react drop zone for multiple files upload.
When i add file with the "add" button or "click", submit data are ok.
But, when i add files using drag'n drop, if i submit, i have no files in the request...
The html markup :
Any help welcome ! thanks a lot
The text was updated successfully, but these errors were encountered: