, поэтому я сомневаюсь, как загрузить изображение с помощью Alamofire, из того, что исследователи используют здесь решения https://github.com/Alamofire/Alamofire/issues/110. Однако мой локон запрос должен выглядеть следующим образом:Загрузить изображение, используя alamofire
curl -X POST "api.local.app.com:9000/1/media/upload" -F “[email protected]” -H “Authorization: Alpha ahudhasiadoaidjiajdiudaiusdhuiahdu” -v
Я пытаюсь сделать это таким образом:
Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization" : "Alpha \(userToken)" ]
var fileManager = NSFileManager()
var tmpDir = NSTemporaryDirectory()
let filename = "tempPicture.png"
let path = tmpDir.stringByAppendingPathComponent(filename)
var error: NSError?
let imageData = UIImagePNGRepresentation(image)
fileManager.removeItemAtPath(path, error: nil)
println(NSURL(fileURLWithPath: path))
if(imageData.writeToFile(path,atomically: true)){
println("Image saved")
}else{
println("Image not saved")
}
Alamofire.upload(.POST, databaseURL + "/media/upload", NSURL(fileURLWithPath: path)!).progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println(String(totalBytesWritten) + "/" + String(totalBytesExpectedToWrite))
}
.responseJSON { (request, response, data, error) in
Из того, что я понимаю, что это должно работать, однако Аламо возвращает
Error: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 2.) UserInfo=0x7e16d9e0 {NSDebugDescription=Invalid value around character 2.}).
Проверьте эту тему. http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire – Tobias
Также это может быть полезно: https://medium.com/@creativewithin/uploading-a-file-in-swift -via-post-multipart-form-data-93dd1001f9e5 – Tobias
Thx каждый, с помощью каждого ответа я понимаю, что проблема была во мне при создании заголовка. –