My react-dropzone 'accept': { .. } parameter seems to be totally ignored when I am uploading files.
My useDropzone({}):
const {getRootProps, getInputProps, isDragActive} = useDropzone({
onDrop,
noClick: true,
'accept': {
'video/mp4': ['.mp4', '.MP4'],
},
})
My onDrop Callback:
const onDrop = useCallback((acceptedFiles, rejectedFiles) => {
let test = acceptedFiles.length || rejectedFiles.length
? `Accepted ${acceptedFiles.length}, rejected ${rejectedFiles.length} files`
: "Try dropping some files.";
console.log(test);
if (acceptedFiles.length > 0) {
setSelectedFiles(acceptedFiles);
}
acceptedFiles.forEach((file, index, array) => {
const reader = new FileReader()
reader.onabort = (event) => {
console.log('file reading was aborted')
}
reader.onerror = (event) => {
console.log('file reading has failed')
}
reader.onload = (event) => {
// Do whatever you want with the file contents
const binaryStr = reader.result
console.log(binaryStr)
}
reader.readAsArrayBuffer(file)
})
}, [])
The code:
let test = acceptedFiles.length || rejectedFiles.length
? `Accepted ${acceptedFiles.length}, rejected ${rejectedFiles.length} files`
: "Try dropping some files.";
always returns:
Accepted 1, rejected 0 files
no matter what, rejected will always be 0 even when I uploaded pdf, jpg, txt etc
Here is the codesandbox Link: https://codesandbox.io/s/kind-frost-zmyhd8?file=/pages/index.js
Anyone knows what is wrong with my code?
According to documentation you need to provide accept prop like below (without quotes) :
useDropzone({
accept: {
'video/mp4': ['.mp4', '.MP4'],
}
})
Here is working solution.