我正在尝试将图片上传到我的后端服务器。创建新记录但未上传任何图像:
func uploadDish(dish:Dish, completionHandler: ((AnyObject?, ErrorType?) -> Void)){
let urlString = "http://backend.com/v1/dish"
SwiftSpinner.show("Uploading Dish...")
Alamofire.upload(
.POST,
urlString,
multipartFormData: { multipartFormData in
if let image = self.image {
if let imageData : NSData = UIImageJPEGRepresentation(image, 0.1){
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "testesttest.jpg", mimeType: "image/jpeg")
}
}
multipartFormData.appendBodyPart(data: "foo".dataUsingEncoding(NSUTF8StringEncoding)!, name :"name")
multipartFormData.appendBodyPart(data: "bar".dataUsingEncoding(NSUTF8StringEncoding)!, name :"address")
multipartFormData.appendBodyPart(data: "1".dataUsingEncoding(NSUTF8StringEncoding)!, name :"priceRange")
multipartFormData.appendBodyPart(data: "\(234)".dataUsingEncoding(NSUTF8StringEncoding)!, name :"lat")
multipartFormData.appendBodyPart(data: "\(234)".dataUsingEncoding(NSUTF8StringEncoding)!, name :"lng")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
print(upload.debugDescription)
break
case .Failure(let encodingError):
print(encodingError)
break
}
}
)
SwiftSpinner.hide()
}
它适用于Postman:
答案 0 :(得分:0)
这实际上是正确的,发现bug在后端。
以上方式是您发布图片的方式