let parameters = [
"profile_picture" : "\(profilePictureData)",
]
Alamofire.request(.POST, UPDATE_PROFILE_PICTURE_URL + "token=\(userToken)", parameters: parameters, encoding: .JSON, headers: AUTHENTICATION_HEADER).response { (request, response, data, errorType) -> Void in
if(data != nil) {
let responseStr:NSString = NSString(data:data!, encoding:NSUTF8StringEncoding)!
print(responseStr);
let resultObject: APIResults = APIResults(JSONDecoder(data!));
let responseCode: Int = Int(resultObject.code!)!;
if(responseCode == 200) {
succeeded = true;
}
}
}
到目前为止,这是我的代码,我试图将我的profilePictureData发送到服务器NSData
,就像我通常发送字符串或者Int一样。我将profilePictureData放到body参数中。这总是失败,服务器返回错误。
我应该如何将我的图像数据发送到服务器?
请帮忙。谢谢
答案 0 :(得分:1)
将此作为模型使用。只需相应地编辑它。
Alamofire.upload(.POST,
URLString: "https://example.com/api/v1/users/\(user_id)/profile_pic.json?auth_token=\(auth_token)",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "avatar", fileName: "avatar_img.png", mimeType: "image/png")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success (let upload, _, _):
upload.responseJSON { request, response, data, error in
// Do whatever
}
case .Failure (let encodingError):
//Realistically, I don't expect this to ever trigger, but I guess if the user uses some very weird image format...
}
})