I'm following this guys Martin Lasek Tutorials and now i'm at "image upload". It seems that no one has the answer to the question "How do you upload images i Vapor 3"
Db connection is ok, all the other values are saved.
This is mye create method:
func create(_ req: Request) throws -> Future<Response> {
return try req.content.decode(Question.self).flatMap { question in
return question.save(on: req).map { _ in
return req.redirect(to: "/form")
}
}
}
and Model:
final class Question: PostgreSQLModel {
var id: Int?
var questionText: String
var answers: [String]
var theme: String?
var imageName: String?
var imageData: File?
init(id: Int? = nil, questionText: String, answers: [String], theme: String, imageName: String?, imageData: File?) {
self.id = id
self.questionText = questionText
self.answers = answers
self.theme = theme
self.imageName = imageName
self.imageData = imageData
}
}
and Leaf template:
<form action="/save" method="POST" enctype="multipart/form-data" id="upload-form">
<input type="file" accept="image/png,image/jpg" name="image">
<input class="btn btn-success btn-block" type="submit" value="Legg til">
</form>
I know a method for managing files is needed and the raw image bytes to,
But how do i get there?