最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple File Upload with React JS - Stack Overflow

programmeradmin0浏览0评论

I am trying to figure out how to loop through multiple file uploads in React JS.

Ultimately I want to be able to loop through each file so that I can tell if only PNG, JPG, and MP3 files are being uploaded. I also want PNG/JPG files to be restricted to 5MB and MP3 files to be restricted to 2MB.

So far, I cannot figure out why I can access one file rather than an array of files.

<input id="file" type="file" onChange={this.handleChange.bind(this)} required multiple />

My handleChange function looks something like this:

handleChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;

     this.setState({
         [id]: value
     });

    console.log(id)
    console.log(value)
}

When I select more than one file, I only get one showing up. For example, the two console lines above produce the following:

file
C:\fakepath\My Secret Document.docx

Why is only a single value stored in value? How can I properly loop through each file to check its size and type? I am not interested in using JQuery.

I am trying to figure out how to loop through multiple file uploads in React JS.

Ultimately I want to be able to loop through each file so that I can tell if only PNG, JPG, and MP3 files are being uploaded. I also want PNG/JPG files to be restricted to 5MB and MP3 files to be restricted to 2MB.

So far, I cannot figure out why I can access one file rather than an array of files.

<input id="file" type="file" onChange={this.handleChange.bind(this)} required multiple />

My handleChange function looks something like this:

handleChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;

     this.setState({
         [id]: value
     });

    console.log(id)
    console.log(value)
}

When I select more than one file, I only get one showing up. For example, the two console lines above produce the following:

file
C:\fakepath\My Secret Document.docx

Why is only a single value stored in value? How can I properly loop through each file to check its size and type? I am not interested in using JQuery.

Share Improve this question asked May 22, 2017 at 23:59 kojow7kojow7 11.4k20 gold badges92 silver badges146 bronze badges 1
  • What you want to check is event.target.files – Joshua Terrill Commented May 23, 2017 at 0:06
Add a comment  | 

1 Answer 1

Reset to default 20

The files are contained in a FileList, inside event.target.files, you can do Array.from(event.target.files) and got an array with all the files selected.

More information about FileList

发布评论

评论列表(0)

  1. 暂无评论