Chocoford
Chocoford

Reputation: 311

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?

Upvotes: 3

Views: 596

Answers (1)

imike
imike

Reputation: 5656

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]
}

Upvotes: 3

Related Questions