[英]Image Upload in Vapor 3 using PostgreSQL
I'm following this guys Martin Lasek Tutorials and now i'm at "image upload". 我正在关注这些家伙Martin Lasek Tutorials,现在我正在“图片上传”。 It seems that no one has the answer to the question "How do you upload images i Vapor 3"
似乎没人能回答“如何上传i Vapor 3图像”的问题
Db connection is ok, all the other values are saved. Db连接正常,所有其他值都保存。
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? 但是我怎么去那里?
This uses automatic decoding of the multi-part form: 这使用多部分表单的自动解码:
router.get("upload") {
request -> Future<View> in
return try request.view().render("upload")
}
struct ExampleUpload: Content {
let document: File
}
// this saves the file into a Question
router.post(ExampleUpload.self, at:"upload") {
request, upload -> Future<HTTPResponseStatus> in
let question = try Question()
question.imageData = upload.document.data
question.imageName = upload.document.filename
return question.save(on:request).transform(to: HTTPResponseStatus.ok)
}
The upload.leaf
file is: upload.leaf
文件是:
<form method="POST" enctype="multipart/form-data">
<input type="file" name="document" />
<input type="submit" value="Send" />
</form>
Using the type File
enables the local filename of the uploaded file to be accessed as well as the file data. 使用
File
类型可以访问上载文件的本地文件名以及文件数据。 If you add in the rest of the Question fields to the ExampleUpload structure, you can use the route to capture the whole form's fields. 如果将其余的Question字段添加到ExampleUpload结构中,则可以使用该路径捕获整个表单的字段。
I've managed to get the image data by doing this: 我设法通过这样做获取图像数据:
struct ImageRequest: Content {
var imageData: Data
}
func analyizeImage(_ request: Request) throws -> Future<Response> {
return try request.content.decode(ImageRequest.self).flatMap(to: Response.self, { [weak self] imageRequest in
guard let image = NSImage(data: imageRequest.imageData) else {
throw Abort(.badRequest)
}
// Do what you want with your image
})
}
My form looked like this: 我的表格看起来像这样:
<form method="POST" enctype="multipart/form-data">
<input type="file" name="imageData" />
<input type="submit" value="Send" />
</form>
In order for req.content.decode(Question.self)
to work, your model have to conforms to the Content
protocol which is Vapor encapsulation of Codable
+ other encoding/decoding stuff. 为了使
req.content.decode(Question.self)
能够工作,你的模型必须符合Content
协议,即Codable
+其他编码/解码内容的Vapor封装。
Did you add it to your Question
model? 你有没有把它添加到你的
Question
模型中?
something like that: 类似的东西:
extension Question: Content {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.