ios - 使用alamofire上传图片

标签 ios swift curl afnetworking alamofire

所以我的疑问是如何使用 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/

相关文章:

ios - 循环函数中的异步调用。仅在异步完成时返回

ios - 如何向部分和分页部分添加两个新单元格?

ios - 如何在表格 View 单元格中使用开关?

php - 无法通过 HTTPS 获取 file_get_contents 或 cURL

ios - 如何在 iOS 设备上使用 appium 自动化不是我的应用程序

ios - 无法在 ios 上使用 AdMob 获取任何测试广告

objective-c - 为什么我的数据可以成功写入 iPhone 模拟器上的文件,而不是我的设备?

ios - swift 中的统一响应/错误处理

ios - 将 cURL 命令行转换为 Swift cURL 不起作用

php - 为什么 cURL 返回一个空字符串?