所以我的疑问是如何使用 Alamofire 上传图像,研究人员使用这里的解决方案 https://github.com/Alamofire/Alamofire/issues/110 .但是我的 curl 请求应该是这样的:
curl -X POST "api.local.app.com:9000/1/media/upload" -F “picture=@filename.png” -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
根据我的理解,这应该可以工作,但是 Alamo 会返回
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.}).
最佳答案
我已经提供了详细的答案here .您的服务器很可能希望图像是多部分形式的数据编码,这就是它被拒绝的原因。该链接中提供了许多详细信息以及后续步骤。
关于ios - 使用alamofire上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997847/