简体   繁体   中英

How to handle multiple images upload in vapor 4?

Here is the Html codes:

<form method="POST" action="/create" enctype="multipart/form-data">
  <div class="mb-3">
    <label for="imgs">Images</label>
    <input type="file" accept="image/*" name="imgs" id="imgs" multiple/>
  </div>
</form>

And Codes below are vapor 4. I got an empty array if:

struct Request: Content {
      let imgs: [Data] //or [File]
}

I got a whole Data if:

struct Request: Content {
      let imgs: Data
}

But how to split a Data to multiple images?

By standard for multiple files name should contain []

Try this form

<form method="POST" action="/create" enctype="multipart/form-data">
  <div class="mb-3">
    <label for="imgs">Images</label>
    <input type="file" accept="image/*" name="imgs[]" id="imgs" multiple/>
  </div>
</form>

and then in Vapor

struct Request: Content {
    let imgs: [File] //or [Data]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM