温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - How can I make the selected file type acceptable only as docx, using FilePond?
filepond javascript node.js reactjs

javascript - 如何使用FilePond使选定的文件类型仅作为docx接受?

发布于 2020-08-16 22:24:28

如何使选定的文件类型仅接受docx我希望所选文件docx具有扩展名。我应该采用哪种过滤?我怎样才能做到这一点?

React 代码示例

return (
    <>
        <Form className='orange-color ml-2'>
            <FilePond
                ref={ref => (ref)}
                allowFileEncode={true}
                allowMultiple={false}
                oninit={() =>
                    console.log("FilePond "+formKey.toString()+" has initialised")
                }

                onupdatefiles={(fileItems) => {
                    const file = fileItems.map(fileItem => fileItem.file)

                    if (file[0]) {
                        const reader = new FileReader();
                        reader.readAsDataURL(file[0]);

                        reader.onload = (event) => {
                            const convertedResult = event.target.result

                            if (convertedResult) {
                                const regex = '(.*)(base64,)(.*)';
                                const matches = convertedResult.match(regex);
                                const val = matches[3];

                                changeSelected(val)
                            }
                        };
                    }
                }

                }
            />
            <Form.Check>
                <Form.Check.Input required
                                  checked={input.value !== null}
                                  style={{display:'none' }}
                />
                <Form.Control.Feedback type="invalid" >Required</Form.Control.Feedback>
            </Form.Check>
        </Form>
    </>
)

查看更多

提问者
okoreni
被浏览
9
Mystik Developed 2020-05-04 23:27

文件类型验证插件可处理对错误类型文件的阻止。当基于输入类型文件创建FilePond实例时,此插件将自动解释accept属性值。

https://github.com/pqina/filepond-plugin-file-validate-type