Does fetch support multiple file upload natively?

前端 未结 2 1515
终归单人心
终归单人心 2021-01-07 12:05

Summary

I am trying to set my FormData properly using javascript.

I need to be able to upload jpg/png, but I might need to upload

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 12:31

    If you want multiples file, you can use this

    var input = document.querySelector('input[type="file"]')
    
    var data = new FormData()
    for (const file of input.files) {
      data.append('files',file,file.name)
    }
    
    fetch('http://localhost:8080/api/upload/multi', {
      method: 'POST',
      body: data
    })
    

提交回复
热议问题