【问题标题】:Upload picture using alamofire使用 alamofire 上传图片
【发布时间】:2015-07-11 22:23:48
【问题描述】:

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

错误:可选(错误域=NSCocoaErrorDomain Code=3840“ 操作无法完成。 (可可错误 3840。)”(无效值 围绕字符 2.) UserInfo=0x7e16d9e0 {NSDebugDescription=Invalid 字符 2.}) 周围的值。

【问题讨论】:

标签: ios swift curl afnetworking alamofire


【解决方案1】:

我已经提供了详细的答案here。您的服务器很可能希望图像是多部分表单数据编码的,这就是它被拒绝的原因。该链接中提供了许多详细信息以及后续步骤。

【讨论】:

  • 所以他们的问题是我为我的 api 调用创建了错误形式的标头。但是谢谢你的帮助。
  • @cnoon 你是来自 github Alamofire 的同一个 cnoon 吗?你不是 Alamofire 项目的负责人吗?如果是这样,感谢您投入的所有工作。
猜你喜欢
  • 2015-10-11
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2019-05-18
  • 2017-05-20
  • 2019-11-24
相关资源
最近更新 更多