繁体   English   中英

使用 customRequest Ant Design (antd) 上传文件停留在“上传”状态

[英]Upload file stuck on 'uploading' status with customRequest Ant Design (antd)

在使用 Ant Design (antd) 上传文件时,我真的需要您的帮助。 我不需要上传组件的请求操作,因此我在 customRequest 中使用 onSuccess() function 来跳过获取,但 onChange 方法的状态仅停留在“上传”上。 它不会 go 进入“完成”状态。 我需要你的帮助,请

沙盒链接: https://codesandbox.io/s/runtime-platform-znow?file=/src/App.js

    import React, { useState } from "react";
    import "./styles.css";
    import { Button, Upload } from "antd";

    export default function App() {
      const [fileList, setFileList] = useState([]);

      const fileProps = {
        action: "",
        name: "file",
        multiple: true,
        customRequest(e) {
          e.onSuccess();
        },
        onChange(info) {
          const { status } = info.file;
          console.log(status); // always 'uploading'
          if (status === "done") {
            // this part is unreachable
            let fileList = [...info.fileList];
            setFileList(fileList);
          }
        },
        onRemove: (file) => {
          setFileList((prevState) => prevState.filter((d) => d.uid !== file.uid));
        }
      };

      return (
        <div className="App">
          <Upload {...fileProps} fileList={fileList}>
            <Button type="primary">Attach file</Button>
          </Upload>
        </div>
      );
    }

更新antd版本后我遇到了类似的问题。 这是我如何在不发送请求的情况下解决此问题的代码。

    const handleChange = useCallback((info) => {
    if (info.file.status === 'uploading') {
        setImage({ loading: true, image: null });
        info.file.status = 'done';
    }
    if (info.file.status === 'done') {
        getBase64(info.file.originFileObj, (imageUrl) => {
            const img = new Image();
            img.src = imageUrl;
            img.addEventListener('load', function () {
                setImage({ loading: false, image: imageUrl });
                setFileList([{ ...info.fileList[0] }]);
            });
        });
    }
}, []);

内部条件(info.file.status === 'uploading')我已经更改了info.file.status === 'done'并且令人惊讶的是它可以工作。 可能有更好的解决方案来解决这个问题,但这可能会有所帮助

请在 function onChange(info) 中设置 FileList(fileList)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM