超大文件传送:如何使用Dropzone组件拒绝超大文件

我使用React-DropzoneDropzone组件与 Nextjs 和 TypeScript。

如果文件大小超过 30MB(30000000字节),我想拒绝上传。

目前,当将大文件放入区域时-出现此错误:

我看到有一个名为onDropRejected的属性与Dropzone组件在这个documentation中使用,但是我们如何使用这个而不是像上面那样遇到错误?

下面是我的 UI 的样子:

My code:
type Props = {
  name?: string;
  isError?: boolean;
  onChange?: (id: string) => void;
};
export const FileUploader = ({ name, isError, onChange }: Props) => {
  const [uploading, setUploading] = useState(false);
  const [nameFile, setNameFile] = useState<string>('File format: CSV Maximum upload size: 30MB');
  const [currId, setCurrId] = useState('');
  useEffect(() => {
    name && setNameFile(name);
  }, [name]);
  const handleDrop = async (acceptedFiles: File[]) => {
    setUploading(true);
    const file = acceptedFiles[0];
    const res = await AssetA.create(file, AssetResourceType.marketingAction);
    if (res.id) {
      setNameFile(file.name);
      onChange?.(res.id);
      currId && (await AssetA.remove(currId));
      setCurrId(res.id);
    }
    setUploading(false);
  };
  return (
    <div>
      <Dropzone
        onDrop={handleDrop}
        multiple={false}
        accept={['image/*', 'text/csv']}
        disabled={uploading}
        maxSize={30000000}>
        {({ getRootProps, getInputProps }) => (
          <div
            {...getRootProps({ clName: 'dropzone' })}
            clName='flex items-center w-full h-40 text-center'>
            <input {...getInputProps()} />
            <div
              clName={clNames(
                'rounded flex h-full items-center justify-center flex-col flex-1 border border-dashed text-gray-700 mr-2.5 py-6',
                isError ? 'border-danger' : 'border-gray'
              )}>
              <Icon name='upload' size={20} />
              <div clName='mb-2.5 text-medium'>Drag and drop to upload</div>
              <on
                type='on'
                clName='px-2.5 border-gray-700 border rounded-full text-small px-3 py-0.5'>
                Select file
              </on>
              <div clName='mt-2 text-gray-500 text-small'>{nameFile}</div>
            </div>
          </div>
        )}
      </Dropzone>
    </div>
  );
};
2

您可以添加一个函数验证器:

const fileValidator = (file) => {
if (file.size > 30000000) {
  return {
    code: "size-too-large",
    message: `file is larger than 30MB`,
  };
}
return null;
}

然后,在您的 useDropZone 中添加您的函数:

 const { getRootProps, getInputProps, isDragAccept, isDragReject } =
useDropzone({
  onDrop,
  validator: fileValidator,
});

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(156)
事业编岗位代码:Logcat中编舞信息的含义
上一篇
电脑管家不显示cpu温度:Windows CPU温度
下一篇

相关推荐

  • android 视频编码深入理解MediaCodec API

    Android 视频编码是指将原始视频数据经过压缩编码后,生成新的视频数据,以便减少视频文件的体积,提高传输速度,以及更好地在 Android 设备上播放。…

    2023-01-13 10:58:18
    0 72 29
  • coremail论客邮箱Coremail论客邮箱

    Coremail论客邮箱是一款专业的企业邮箱服务,可以满足企业对安全、可靠性和高效性的要求。它拥有强大的安全性能,可以提供多种安全保护,包括防止邮件被窃取、拦截恶意邮件、防止跨站脚本攻击等。此外,它还支持多种企业级功能,如组织架构管理、收发邮件管理、文件共享管理、联系人管理等,可以帮助企业提高工作效率,提升企业形象。…

    2023-02-25 04:36:55
    0 23 53
  • linux 编译静态库:```ar cr libtest.a *.o```4. 完成!

    我们要创建一个源文件,比如:mylib.c,内容如下:#include…

    2023-03-19 08:46:39
    0 23 76
  • canvas下载安装:Unleash the Power of Canvas to Create Amazing Visuals

    Canvas是一种HTML5技术,可以在网页上创建和绘制2D图形。它是一个JavaScript API,可以使用JavaScript代码来绘制图形,并且可以添加各种效果,如阴影,渐变,动画等。…

    2023-03-08 00:48:14
    0 58 95
  • class定位:The Benefits of Using Class Selectors for Element Locati

    示例示例class定位是一种CSS布局技术,用于指定HTML元素的位置,可以使元素放置在页面的任何位置。代码示例:…

    2023-03-06 07:16:44
    0 57 29
  • security code怎么填保护您的数据和隐私

    示例示例code是一种防止自动提交表单的安全措施,通常会在表单中显示一个图片,用户需要输入图片中显示的字符。以下是一个简单的 code代码示例:…

    2023-03-20 09:31:54
    0 47 21
  • css 背景色 透明:Let Your True Colors Shine Through!

    示例示例CSS 背景色透明可以使用 rgba 函数来实现,其中 a 代表 alpha 通道,取值范围为 0-1,0 代表完全透明,代表完全不透明。示例代码:…

    2023-02-13 01:56:46
    0 62 25
  • carlife离线地图下载实现更便捷的出行体验

    CarLife离线地图下载是一种让用户可以在离线状态下使用CarLife地图的方式。它通过将地图数据存储在本地,从而使用户可以在没有网络连接的情况下使用CarLife地图。…

    2023-02-05 10:21:52
    0 21 82

发表评论

登录 后才能评论

评论列表(40条)