簡體   English   中英

react-dropzone 接受所有上傳的文件類型,而不是“accept”參數中指定的文件類型

[英]react-dropzone accepts all uploaded file types instead of specified file type(s) in "accept" parameter

當我上傳文件時,我的react-dropzone 'accept': { .. }參數似乎完全被忽略了。

我的useDropzone({})

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

我的onDrop回調:

    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)

        })


    }, [])

編碼:

        let test =  acceptedFiles.length || rejectedFiles.length
            ? `Accepted ${acceptedFiles.length}, rejected ${rejectedFiles.length} files`
            : "Try dropping some files.";

總是返回: Accepted 1, rejected 0 files

無論如何,即使我上傳了pdfjpgtxt等, rejected也總是0

這是代碼框鏈接:https ://codesandbox.io/s/kind-frost-zmyhd8?file=/pages/index.js

有人知道我的代碼有什么問題嗎?

根據文檔,您需要提供如下所示的接受道具(不帶引號):

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

是工作解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM