Vapor upload multiple files at once

2020-07-11 05:03发布

问题:

I want to upload multiple images in one POST request. Currently, the part of my request related to the file upload is taking one file and looks like this:

return try req.content.decode(File.self).flatMap(to: Image.self) { (file) in
    try file.data.write(to: URL(fileURLWithPath: DirectoryConfig.detect().workDir + localImageStorage + file.filename))
    return try Image(userID: user.requireID(), url: imageStorage + file.filename, filename: file.filename).save(on: req)
}

This works just fine. Now, I tried to change .decode(File.self) to .decode([File].self), and do a loop for all files.
When trying to upload images using the data[] parameter in Postman, I get the following error:

Nested form-data decoding is not supported.

How do I solve this?

回答1:

Example below works well, tested multiple times already