Image Upload in Vapor 3 using PostgreSQL

后端 未结 3 1700
没有蜡笔的小新
没有蜡笔的小新 2021-01-04 11:16

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\"

3条回答
  •  温柔的废话
    2021-01-04 11:45

    I've managed to get the image data by doing this:

    struct ImageRequest: Content {
        var imageData: Data
    }
    
    func analyizeImage(_ request: Request) throws -> Future {
        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:

提交回复
热议问题