当前位置:   article > 正文

Antd-Upload组件设置fileList属性时onChnage只执行到“uploading“状态引发的一些问题的解决方案_antd upload filelist

antd upload filelist

需求:文件上传失败时不需要回显。

思路:添加fileList属性,绑定一个列表,上传成功的文件才加入列表中,代码如下:
 

  1. ...
  2. const [ displayFileList, setDisplayFileList ] = useState<any[]>([]);
  3. ...
  4. <Upload
  5. action="xxx"
  6. accept=".xls*, .csv"
  7. ...
  8. fileList={displayFileList}
  9. ...
  10. onChange={resp => {
  11. const _file = resp.file || {};
  12. if(_file.status!=="done") return
  13. // 上传成功
  14. if (_file.response?.status == 0) {
  15. const uid = _file.response?.uid || "";
  16. handleFileListChange([uid]);
  17. }else{
  18. notification["error"]({
  19. message: _file.response?.msg||"上传文件失败,请重新上传"
  20. });
  21. }
  22. }}
  23. >

问题点(坑):根本走不了done状态

解决方式:不废话,直接上关键代码

  1. ...
  2. const [ displayFileList, setDisplayFileList ] = useState<any[]>([]);
  3. ...
  4. <Upload
  5. action="xxx"
  6. accept=".xls*, .csv"
  7. ...
  8. fileList={displayFileList}
  9. ...
  10. onChange={resp => {
  11. const _file:any = resp.file || {};
  12. if (_file.status == "uploading") {
  13. // 在这里设置一下需要展示的文件才会继续走到done状态
  14. setDisplayFileList([_file]);
  15. return;
  16. }else if (["removed","error"].indexOf(_file.status) !== -1) {
  17. // "removed","error"状态时也设置一下
  18. setDisplayFileList([]);
  19. return;
  20. }
  21. // done状态
  22. // 后端返回状态码为0证明上传成功
  23. if (_file.response?.status == 0) {
  24. const uid = _file.response?.uid || "";
  25. } else {
  26. notification["error"]({
  27. message: _file.response?.msg || "上传文件失败,请重新上传"
  28. });
  29. }
  30. }}
  31. >

结果:思路基本如此,细节尚需打磨。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/339062
推荐阅读
相关标签
  

闽ICP备14008679号