Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用react中的react dnd库上载文件功能_Reactjs_React Dnd - Fatal编程技术网

Reactjs 使用react中的react dnd库上载文件功能

Reactjs 使用react中的react dnd库上载文件功能,reactjs,react-dnd,Reactjs,React Dnd,我想使用react dnd在react中创建一个文件上载功能,用户应该能够通过将文件放到div中来上载文件- <div className="file-select" onDragOver={dragOver} onDragEnter={dragEnter} onDragLeave={dragLeave} onDrop={fileDrop} onClick={fileInputClicked} > &

我想使用react dnd在react中创建一个文件上载功能,用户应该能够通过将文件放到div中来上载文件-

  <div
    className="file-select"
    onDragOver={dragOver}
    onDragEnter={dragEnter}
    onDragLeave={dragLeave}
    onDrop={fileDrop}
    onClick={fileInputClicked}
  >
    <h4>Select File</h4>
    <input
      ref={fileInputRef}
      className="file-input"
      type="file"
      multiple
      onChange={filesSelected}
    />
  </div>
这只是一个框架——您需要实现这些方法- 考虑验证-支持哪些文件等

此外,您还可以实现文件预览、删除等功能,因此在这种情况下,解决方案的设计或至少是概念非常重要


例如,我将此功能用于多选:

  const [selectedFiles, setSelectedFiles] = useState([]);
  const [validFiles, setValidFiles] = useState([]);
  const [unsupportedFiles, setUnsupportedFiles] = useState([]);


const handleFiles=(文件)=>{
for(设i=0;i[…prevArray,files[i]]);
}否则{
文件[i]。无效=true;
setSelectedFiles((prevArray)=>[…prevArray,files[i]]);
setErrorMessage(“不允许使用文件类型”);
setUnsupportedFiles((prevArray)=>[…prevArray,files[i]]);
}
}
handleFileData(文件);
};

为此,您可以使用react dropzone软件包

npm install --save react-dropzone
或:

使用钩子的示例代码段

import React, {useCallback} from 'react'
import {useDropzone} from 'react-dropzone'
 
function MyDropzone() {
  const onDrop = useCallback(acceptedFiles => {
    // Do something with the files
  }, [])
  const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop})
 
  return (
    <div {...getRootProps()}>
      <input {...getInputProps()} />
      {
        isDragActive ?
          <p>Drop the files here ...</p> :
          <p>Drag 'n' drop some files here, or click to select files</p>
      }
    </div>
  )
}
import React,{useCallback}来自“React”
从“react dropzone”导入{useDropzone}
函数MyDropzone(){
const onDrop=useCallback(acceptedFiles=>{
//对这些文件做些什么
}, [])
const{getRootProps,getInputProps,isDragActive}=useDropzone({onDrop})
返回(
{
是主动的吗?
将文件放到这里…

: 将一些文件拖放到此处,或单击以选择文件

} ) }
使用包装器组件的示例代码段

import React from 'react'
import Dropzone from 'react-dropzone'
 
<Dropzone onDrop={acceptedFiles => console.log(acceptedFiles)}>
  {({getRootProps, getInputProps}) => (
    <section>
      <div {...getRootProps()}>
        <input {...getInputProps()} />
        <p>Drag 'n' drop some files here, or click to select files</p>
      </div>
    </section>
  )}
</Dropzone>
从“React”导入React
从“react Dropzone”导入Dropzone
console.log(acceptedFiles)}>
{({getRootProps,getInputProps})=>(
将一些文件拖放到此处,或单击以选择文件

)}
使用onDrop的回调,您可以获得包含文件的acceptedFiles数组。您还可以限制大小、允许多个或仅允许一个文件以及要接受的文件类型

下面是一个示例片段,允许多个10MB的图像,并且只允许png、jpg、jpeg的扩展

<Dropzone 
 multiple={true}
 minSize={0}
 maxSize={10485760}
 accept="image/png,image/jpg,image/jpeg"
 onDrop={acceptedFiles => console.log(acceptedFiles)}>
      {({getRootProps, getInputProps}) => (
        <section>
          <div {...getRootProps()}>
            <input {...getInputProps()} />
            <p>Drag 'n' drop some files here, or click to select files</p>
          </div>
        </section>
      )}
    </Dropzone>
console.log(acceptedFiles)}>
{({getRootProps,getInputProps})=>(
将一些文件拖放到此处,或单击以选择文件

)}
签出-

您可以用很少的代码开始,如下所示:

从“@rpldy/Uploady”导入上传;
从“@rpldy/upload drop zone”导入UploadDropZone;
常量应用=()=>(
将文件拖放到此处
);
yarn add react-dropzone
import React, {useCallback} from 'react'
import {useDropzone} from 'react-dropzone'
 
function MyDropzone() {
  const onDrop = useCallback(acceptedFiles => {
    // Do something with the files
  }, [])
  const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop})
 
  return (
    <div {...getRootProps()}>
      <input {...getInputProps()} />
      {
        isDragActive ?
          <p>Drop the files here ...</p> :
          <p>Drag 'n' drop some files here, or click to select files</p>
      }
    </div>
  )
}
import React from 'react'
import Dropzone from 'react-dropzone'
 
<Dropzone onDrop={acceptedFiles => console.log(acceptedFiles)}>
  {({getRootProps, getInputProps}) => (
    <section>
      <div {...getRootProps()}>
        <input {...getInputProps()} />
        <p>Drag 'n' drop some files here, or click to select files</p>
      </div>
    </section>
  )}
</Dropzone>
<Dropzone 
 multiple={true}
 minSize={0}
 maxSize={10485760}
 accept="image/png,image/jpg,image/jpeg"
 onDrop={acceptedFiles => console.log(acceptedFiles)}>
      {({getRootProps, getInputProps}) => (
        <section>
          <div {...getRootProps()}>
            <input {...getInputProps()} />
            <p>Drag 'n' drop some files here, or click to select files</p>
          </div>
        </section>
      )}
    </Dropzone>