【问题标题】:store File in Redux and retrieve it在 Redux 中存储文件并检索它
【发布时间】:2020-07-25 13:50:42
【问题描述】:

我正在使用material-ui-dropzone上传一个文件数组并存储在redux中

但是当我从 redux 状态中检索它时,它不是“文件”类型,并且在 devtools 中显示为

现在我尝试读取文件

const reader = new FileReader();
fileName = mydatafromstate[mydatafromstate.length - 1].name
// file reading finished successfully
reader.addEventListener('loadend', function(e) {
  // contents of file in variable
  let filecontent = e.target.result;

  console.log(filecontent);
});
reader.readAsText(mydatafromstate[mydatafromstate.length - 1])


  

我收到以下错误

error validating against api:  TypeError: Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.

我的 redux 状态也是这样的

如何在 Redux 中正确存储 File 并将其作为 File 检索并检索其内容?

【问题讨论】:

    标签: javascript reactjs redux material-ui


    【解决方案1】:

    这可能不是最好的答案,如果可以的话,我只是想提供帮助。 据我所知,我们通过调度来更新 redux 存储。换句话说,您需要将一个动作和一个值传递给 reducer。 我想在成功收到您的文件后,尝试以下操作:

    dispatch{(
        type: "action-name", // give it a proper name like reading-file
        content: "here add your file content", // pass your array 
      });
    

    然后,您需要在 reducer 中定义一个操作来更新您的全局状态。

    对于文件,我认为您可能需要将文件下载到公共或上传文件夹中,然后将文件路径提供给 redux。

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多