[英]How to use vapor for uploading/downloading image?
現在:我正在上傳數據並將其保存到我的公用文件夾中:
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)時,我看不到我的圖像。 我在哪里做錯了什么? 也許還有另一種方法。 和我的公共目錄中的文件看起來不像圖像。 他們只是歸檔。 也許您知道如何像保存圖像一樣?
上傳的圖像將成為多部分請求的一部分,因此您只需要使用包含該圖像的部分。 .bytes
將為您提供整個請求。 我用:
guard let fileBytes = info.formData?["photo"]?.part.body
要么
guard let fileBytes = info?["photo"]?.part.body
如果您不使用表格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.