Short answer
AFAIK You can't
Long answer
Let's see the following code:
<Dropzone
accept="image/*,application/pdf"
maxSize={5000000}
onDrop={this.onDrop}
onDragEnter={this.onDragEnter}
>
{({getRootProps, getInputProps, isDragAccept, isDragReject }) => {
let classes = 'form-control'
let placeholder = <p>Drag files here</p>;
if (isDragAccept) {
classes = `${classes} border-success bg-light`;
placeholder = <p className="text-success">Drop files now</p>;
}
if (isDragReject) {
classes = `${classes} border-danger bg-light`;
placeholder = <p className="text-danger">Some files are not allowed</p>
}
return (
<div {...getRootProps()} style={{ height: '100px' }} className={classes}>
<input {...getInputProps()} />
{placeholder}
</div>
);
}}
</Dropzone>
This snippet changes the style of the dropzone if the file to be dropped meets some requirements (any image type or pdf file and that is less than 5MB of size). If we drag any file over the dropzone the values of acceptedFiles
and rejectedFiles
are both an empty array. The value of draggedFiles
is an array of DataTransferItems which give us the kind
and type
of the file being dragged, for example {kind: "file", type: "application/pdf"}
.
At this point the values of isDragAccept
and isDragReject
are setted based on the type of the file. So, if we drag an image or a pdf file the value isDragAccept
is set to true
, the text Drop files now
will be shown in the dropzone its border will be colored according the class border-success
. But the same will occur if the file is greater than 5MB! Why? This is because we cannot read the file before the drop event.
Let see the event handlers:
onDrop = (acceptedFiles, rejectedFiles, event) => {
console.log(event.dataTransfer.items)
console.log(event.dataTransfer.files)
}
onDragEnter = (event) => {
console.log(event.dataTransfer.items)
console.log(event.dataTransfer.files)
}
The drag events give us a dataTransfer property which is a DataTransfer object. Here we have two properties that are important to us:
DataTransfer.items
This is a DataTransferItemList object which give us a list of DataTransferItems. It's the same object in the draggedFiles
array and we can access this in both handlers: onDragEnter and onDrop.
DataTransfer.files
This holds a FileList object of the drag operation, but it's empty in the Drag events so we can access it only after the drop. For example, in the previous code if we drag and drop some file we get this output for the console log for the events showed:
FileList {length: 0} // dragEnter event
FileList {0: File(30299626), length: 1} // drop event
If we access the element 0 of the File list after the drop we get:
File(30299626) {name: "python_101.pdf", lastModified: 1549327543709, lastModifiedDate: Mon Feb 04 2019 21:45:43 GMT-0300 (Chile Summer Time), webkitRelativePath: "", size: 30299626, …}
Thus, we can only access data of the file After the drop event.