2

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?

yeln
  • 462
  • 2
  • 10
  • 23

3 Answers3

2

According to documentation you need to provide accept prop like below (without quotes) :

useDropzone({
  accept: {
    'video/mp4': ['.mp4', '.MP4'],
  }
})

Here is working solution.

Nemanja
  • 3,295
  • 11
  • 15
0

The documentation now says:

Note that the onDrop callback will always be invoked regardless if the dropped files were accepted or rejected. If you'd like to react to a specific scenario, use the onDropAccepted/onDropRejected props.

However the function signature is misleading because the parameter is named acceptedFiles even though it's a mix of both accepted and rejected files:

function onDrop(acceptedFiles)
kym
  • 818
  • 7
  • 12
0

You can use fileRejections instead of rejectedFiles to achieve this

const {getRootProps, getInputProps, isDragActive, fileRejections, acceptedFiles} = useDropzone({
        onDrop,
        noClick: true,
        'accept': {
            'video/mp4': ['.mp4', '.MP4'],
        },
    })