如何在Redux存储中保存File对象数组?

时间:2019-06-11 02:05:37

标签: reactjs redux react-redux

我想将文件对象数组保存在redux存储中。

这是关于减速器的代码

    case types.SET_PICTURE:
      console.log("action");
      console.log(action.newPictureFiles);
      console.log(action.newPictureDataURLs);
      return {
        ...state,
        inquiryForm: {
          ...state.inquiryForm,
          pictureFiles: [...action.newPictureFiles],
          pictureDataURLs: [...action.newPictureDataURLs]
        }
      };

此代码与分派有关

class Attachment extends Component {
  constructor(props) {
    super(props);
    this.state = {
      pictureFiles: this.props.pictureFiles,
      pictureDataURLs: this.props.pictureDataURLs,
      uploadedURLs: []
    };
    this.onDrop = this.onDrop.bind(this);
  }

  onDrop(pictureFiles, pictureDataURLs) {
    // pictureFiles = [{File object}, {File object}, {File object}]
    console.log("onDrop")
    console.log(pictureFiles);
    console.log(pictureDataURLs);
    this.props.onSetPicture(pictureFiles, pictureDataURLs)
    this.setState({
      pictureFiles: pictureFiles,
      pictureDataURLs: pictureDataURLs,
    });
  }

console.log(action.newPictureFiles)在减速器功能用例类型.SET_PICTURE:之后,可以像这样[File object array in chrome dbg]很好地打印[,redux result]。为什么reducers无法将文件对象保存在redux存储中。以及如何解决?

1 个答案:

答案 0 :(得分:1)

您的action.newPictureFiles有文件,但未显示,因为控制台日志会将文件打印为空obj。

考虑到您的操作,请使用它来记录所有数据。newPictureFiles是文件数组。

action.newPictureFiles.forEach(file => {
    for (var pair of file.entries()) {
        console.log(pair[0] + ', ' + pair[1]);
    }
})

或者只是每个文件

for (var pair of file.entries()) {
    console.log(pair[0]+ ', ' + pair[1]); 
}