我正在使用以下内容来允许用户使用react-dropzone上传头像
const FILE_FIELD_NAME = 'files';
const renderDropzoneInput = (field) => {
const files = field.input.value;
let dropzoneRef;
return (
<div>
<Dropzone
name={field.name}
onDrop={( filesToUpload, e ) => field.input.onChange(filesToUpload)}
ref={(node) => { dropzoneRef = node; }}
accept="image/jpeg, image/png"
maxSize={5242880}
>
{({ isDragActive, isDragReject, acceptedFiles, rejectedFiles }) => {
if (isDragActive) {
return "This file is authorized";
}
if (isDragReject) {
return "This file is not authorized";
}
return acceptedFiles.length || rejectedFiles.length
? `Accepted ${acceptedFiles.length}, rejected ${rejectedFiles.length} files`
: "Try dropping some files.";
}}
</Dropzone>
<button type="button" onClick={() => { dropzoneRef.open() }}>Open File Dialog</button>
{field.meta.touched &&
field.meta.error &&
<span className="error">{field.meta.error}</span>}
{
files && Array.isArray(files) && (
<ul>
{ files.map((file, i) =>
<li key={i}>
<img key={i} style={{width: 50, height: 50}} src={file.preview} alt="preview" />
{file.name}
</li>
)}
</ul>
)}
</div>
);
}..。在我的redux-form中:
<div>
<label htmlFor={FILE_FIELD_NAME}>Files</label>
<Field
name={FILE_FIELD_NAME}
component={renderDropzoneInput}
/>
</div>React-dropzone目前支持maxSize,它拒绝超过5MB的文件。问题是react-dropzone没有告诉用户文件太大。
如果文件超出了允许的maxSize,我如何更新上面的代码来告诉用户?
发布于 2020-09-05 19:02:32
import React, { useState } from "react";
import { useDropzone } from "react-dropzone";
const UploadFile = () => {
const [errors, setErrors] = useState("");
const { getRootProps, getInputProps } = useDropzone({
multiple: false,
onDrop: (acceptedFiles, fileRejections) => {
fileRejections.forEach((file) => {
file.errors.forEach((err) => {
if (err.code === "file-too-large") {
setErrors(`Error: ${err.message}`);
}
if (err.code === "file-invalid-type") {
setErrors(`Error: ${err.message}`);
}
});
});
}
return (
<div{...getRootProps()}>
<input {...getInputProps()} title={title} />
<p style={{ color: "red", padding: 5, margin: 0, fontSize: 14 }}>
{errors}
</p>
</div>
);
};然后,循环访问错误数组以访问“
当multiple设置为'false‘时,这非常有效。对于多个文件错误,我想你必须使用arrayState。我还没有真正调查过。
发布于 2017-12-14 15:28:41
你可以在加载后获得每个文件的当前大小,并与常量进行比较。我不知道文件是否有一个大小的道具,但我想它包含在道具中。代码应如下所示:
const FILE_FIELD_NAME = 'files';
const ErrorMessage = ({ children }) => (
<div
style={{
fontStyle: 'italic',
color: 'red',
}}
>
{children}
</div>
)
const renderDropzoneInput = (field) => {
const files = field.input.value;
let dropzoneRef;
const MAX_SIZE = 5242880;
return (
<div>
<Dropzone
name={field.name}
onDrop={( filesToUpload, e ) => field.input.onChange(filesToUpload)}
ref={(node) => { dropzoneRef = node; }}
accept="image/jpeg, image/png"
maxSize={MAX_SIZE}
>
{({ isDragActive, isDragReject, acceptedFiles, rejectedFiles }) => {
if (isDragActive) {
return "This file is authorized";
}
if (isDragReject) {
return "This file is not authorized";
}
return acceptedFiles.length || rejectedFiles.length
? `Accepted ${acceptedFiles.length}, rejected ${rejectedFiles.length} files`
: "Try dropping some files.";
}}
</Dropzone>
<button type="button" onClick={() => { dropzoneRef.open() }}>Open File Dialog</button>
{field.meta.touched &&
field.meta.error &&
<span className="error">{field.meta.error}</span>}
{
files && Array.isArray(files) && (
<ul>
{ files.map((file, i) =>
<li key={i}>
{file.size > MAX_SIZE ? (
<ErrorMessage>
{'file is too big, try with another file'}
{file.name}
</ErrorMessage>
) : (
<React.fragment>
<img key={i} style={{width: 50, height: 50}} src={file.preview} alt="preview" />
{file.name}
</React.fragment>
)
}
</li>
)}
</ul>
)}
</div>
);
}https://stackoverflow.com/questions/44751990
复制相似问题