簡體   English   中英

使用選擇多個圖像 <input type=“file” /> ,更新狀態以存儲圖片網址,動態添加 <img /> 標簽以在reactjs中顯示它們

[英]select multiple images using <input type=“file” />, update the state to store the image URLs, dynamically add <img /> tags to display them in reactjs

this.state = {
  imageURL: []
};

fileSelectHandler = (event) => {
  this.setState({
    imageURL: [???]
  });
}

<input type="file" multiple onChange={fileSelectHandler} />
<img src={imageURL[0]} />

在這里,我需要根據所選圖像的數量動態添加多個img標簽。 請幫忙!

關於文件更改,您可以在此處嘗試示例代碼

fileSelectHandler = (e) => {
    Array.from(e.target.files).forEach(file => {
      const reader = new FileReader();
      reader.onload = (e) => {
      this.setState({
        images: this.state.images.concat(e.target.result),
      });
    };
    reader.readAsDataURL(file);
  })
}

演示在這里https://codesandbox.io/s/qxxzz1q11j

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM