我正在使用以下内容允许用户使用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,它拒绝超过 5megs 的文件。问题是 react-dropzone 没有告诉用户文件太大。
如何更新上述内容以告诉用户文件是否超过允许的 maxSize?