redux 使用Reactjs选择并上传多个图像

ct3nt3jp  于 2022-11-12  发布在  React
关注(0)|答案(2)|浏览(132)

我正在开发一个需要上传多张图片的应用程序。我正在使用redux-sagaNexjs。现在它正在上传一张图片,但选择了多张图片。
我的代码:

const inputFile = React.useRef(null);

const onChangeFile = event => {
event.stopPropagation();
event.preventDefault();
var image = event.target.files[0];
if (image) {
  var temp = URL.createObjectURL(image);
  onImageSelect && onImageSelect(temp);
  set_disabled(true);
  dispatch(
    post.uploadImage({
      fileObject: image,
      callback: source_url => {
        imageHandler(source_url);
        set_disabled(false);
        event.target.value = null;
        onImageUploaded && onImageUploaded();
      },
    })
  );
}
};

JSX

<>
  <input
    accept='image/*'
    type='file'
    ref={inputFile}
    style={{ display: 'none' }}
    onChange={onChangeFile}
    multiple
  />

  <Box clone color='#666'>
    <IconButton
      size='small'
      onClick={() => {
        inputFile.current.click();
      }}
    >
      <WallpaperOutlinedIcon />
    </IconButton>
  </Box>
</>

我需要添加哪些更改才能上传多张图片,我有点搞不清楚。谢谢

oyjwcjzk

oyjwcjzk1#

从阵列中获取所有文件并尝试上传每个文件:

var images = event.target.files; // Will contain all the images
for(const image of images) {
  // image is single image object
  // do whatever you want
}
xxhby3vn

xxhby3vn2#

我没有代表直接回复你的评论,但是关于这里的另一个答案,使用Promise.all()而不是for循环。

相关问题