如何为 uploading/downloading 图像使用蒸汽?

How to use vapor for uploading/downloading image?

现在:我正在上传数据并将其保存到我的 public 文件夹中:

let info = req.data
            guard let fileBytes = info["photo"]?.bytes
            else {
                var answerJson = JSON();
                try answerJson.set("success", "false");
                return answerJson
            }

            let dir = URL(fileURLWithPath: self.config.workDir).appendingPathComponent("Public/FleaMarketProductImages")
            let dirWithImage = dir.appendingPathComponent("test3.jpg")
            let fileManager = FileManager()
            let data = Data(bytes: fileBytes)
            fileManager.createFile(atPath: dirWithImage.path, contents: data, attributes: nil)

然后在设备上下载这个文件我接下来要做的是:

 Alamofire.download(url).responseData { response in
            if let data = response.result.value {
                completionHandler(data)
            }
        }

我正在获取一些数据,但是当我执行 UIImage(data: data) 时,我看不到我的图像。我哪里做错了什么?也许还有另一种方法可以做到。我的 public 目录中的文件看起来不像图像。他们只是归档。也许你知道如何像图片一样保存它????

上传的图片将是 multi-part 请求的一部分,因此您只需使用包含图片的部分。 .bytes 会给你整个请求。我使用:

guard let fileBytes = info.formData?["photo"]?.part.body

或者

guard let fileBytes = info?["photo"]?.part.body

如果您没有使用表格。