arrays - 无法将文件放入 redux 数组

标签 arrays reactjs file redux react-redux

我正在 redux 数组中上传文件,它似乎正在获取文件作为有效负载,因为它登录控制台但是当我将它推送到数组时,它会放置空对象而不是实际文件,我没有遇到这个问题,所以我不知道是什么这样做是代码:

const uploadImage = (e) => {
  const files = e.target.files;
  console.log(files)
  for(const file of Array.from(files)){
     console.log(file)
     dispatch({
       type: UPLOAD_IMAGE,
       payload: file
     });
  };
};

reducer :
case UPLOAD_IMAGE:
  console.log(action.payload)
  return {
    ...state,
    images: [...state.images, action.payload]
  }

这是有效载荷

enter image description here

这是 redux devtools 扩展

enter image description here

最佳答案

您必须为 Redux 创建新对象首先。
我不知道细节,但把File/BlobRedux与 JS 对象的工作方式不同。如果您有更好的解决方案,请与我分享)

    const file = document.querySelector<HTMLInputElement>(`#file`);
    const blob = file.files[0];
    const blobForRedux = {};

    [
        'lastModified',
        'lastModifiedDate',
        'name',
        'path',
        'preview',
        'size',
        'type',
        'webkitRelativePath',
    ].map((key) => {
        blobForRedux[key] = blob[key];
    });

关于arrays - 无法将文件放入 redux 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59813971/

相关文章:

asp.net - 在数据库或文件中保存长细节?

java - 使用Java访问网络中的文件

c - 长字符串中的段错误

reactjs - 在 Next/React 完成加载之前显示带有百分比的加载横幅

R包创建函数名称和.R/.rd名称

reactjs - Material-ui自动完成警告提供给自动完成的值无效

reactjs - 如何使用 React-Hook-Form 设置 ref 焦点

java - 如何使用另一个类中的字符串数组?

c++ - 所有 k 个子序列的最小值中的最大值

java - 读取一行文本文件,拆分为数组