【发布时间】:2021-01-27 07:10:55
【问题描述】:
如何使用选择、使用 react-dropzone 将一个或多个文件及其描述添加到组件的状态中。
我正在使用 Reactjs、dropzone 和 bootstrap,我想要实现的是:添加一个或多个文件(通过将它们拖动到一个区域),然后查看添加文件的列表和每个文件的选择输入(使用用户定义“类型”的选项)将所有这些保存在一个状态中,然后将该信息发送到 API。 类似于图像中显示的内容:
到目前为止,我的代码根据其扩展名(pdf、xlsx ...)和被拒绝的文件,返回了一个被接受的文件列表,但我不知道如何添加一个选择(带有“type”选项,可以是“summary”、“report”、“test”...)并将其保存为状态,然后将其发送到 API。
我目前使用react-dropzone的代码是这样的:
const baseStyle = {
flex: 1,
display: "flex",
flexDirection: "column",
alignItems: "center",
padding: "20px",
borderWidth: 2,
borderRadius: 20,
borderColor: "#26C2E7",
borderStyle: "dashed",
backgroundColor: "#fafafa",
color: "#c4c4c4",
outline: "none",
transition: "border .24s ease-in-out"
};
const activeStyle = {
borderColor: "#f2f"
};
const acceptStyle = {
borderColor: "#f8f"
};
const rejectStyle = {
borderColor: "#f2f"
};
function InputFiles(props) {
const {
acceptedFiles,
fileRejections,
isDragActive,
isDragAccept,
isDragReject,
getRootProps,
getInputProps
} = reactDropzone.useDropzone({
accept: ".xlsx,.docx,.pdf"
});
const style = React.useMemo(
() => ({
...baseStyle,
...(isDragActive ? activeStyle : {}),
...(isDragAccept ? acceptStyle : {}),
...(isDragReject ? rejectStyle : {})
}),
[isDragActive, isDragReject, isDragAccept]
);
const acceptedFileItems = acceptedFiles.map((file) => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
const fileRejectionItems = fileRejections.map(({ file, errors }) => (
<li key={file.path}>
{file.path} - {file.size} bytes
<ul>
{errors.map((e) => (
<li key={e.code}>{e.message}</li>
))}
</ul>
</li>
));
return (
<section className="container">
{/* <div {...getRootProps({ style })}> */}
<div {...getRootProps({ style })}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
<em>(Only *.pdf , *.xlsx , *.docx files will be accepted)</em>
</div>
<aside>
<h4>Accepted files</h4>
<ul>{acceptedFileItems}</ul>
<h4>Rejected files</h4>
<ul>{fileRejectionItems}</ul>
</aside>
</section>
);
}
ReactDOM.render(<InputFiles />, document.body);
window.onload = function() {
console.log('onload');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.7.2/prop-types.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dropzone/11.2.0/index.js"></script>
我们的目标是得到这样的东西:
添加文件及其描述时,必须以组件的状态保存,目的是点击保存时向API发出POST请求,点击取消时必须删除状态信息
【问题讨论】:
标签: javascript reactjs file drag-and-drop react-dropzone