ios - 从 Alamofire 上传图像中提取响应消息

标签 ios swift response alamofire image-uploading

我需要从使用 Alamofire 将图像上传到服务器的 post 请求中提取 json 响应,并通过以下代码将图像作为 multipartformdata 上传

    Alamofire.upload(
        .POST,
        "http://www.mywebsite.com/api/index.php/profileimg",
        headers: ["Authorization" : "No Auth"],
        multipartFormData: { multipartFormData in
             multipartFormData.appendBodyPart(data: jpegImage1, name: "image1", fileName: "img1", mimeType:  "image/jpeg")
        },
        encodingCompletion: { encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):

                upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
                    dispatch_async(dispatch_get_main_queue()) {
                    }
                }
                upload.validate()
                upload.responseJSON { response in
                print(response.1)
                print(response.2)
                print(response)
                }
            case .Failure(let encodingError):
                print(encodingError)
            }

        }

    )

当我打印响应时,我得到了

    (<NSHTTPURLResponse: 0x165dc0e0> { URL: http://www.mywebsite.com/api/index.php/profileimg } { status code: 200, headers {
    Connection = close;
    "Content-Encoding" = gzip;
    "Content-Type" = "text/html";
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding";
} }), SUCCESS: {
    Path1 = "http://www.mywebsite.com/api/tmpimage/img1";
    Path2 = "http://www.website.com/api/tmpimage/img2";
    Result = success;
})

如何从响应中提取 Path1 和 Path2 值作为字符串?

最佳答案

通过

找到了从响应中获取path1和path2的解决方案
  upload.responseJSON { response in

                print(response.1)
                print(response.2)
                print(response)

                    let dic1 =  (response.2.value as? NSDictionary)?.objectForKey("Path1") as! String
                    print(dic1)
                    let dic2 = (response.2.value as? NSDictionary)?.objectForKey("Path1") as! String
                    print(dic2)

                }

因此可以从响应中提取 path1 和 path2。

关于ios - 从 Alamofire 上传图像中提取响应消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40947391/

相关文章:

ios - 单例和委托(delegate)

ios - RxSwift。结合最新。不是所有的可观察物都发出

ios - 如何检查 iOS 中的深色模式?

ios - 推文未将变量显示为字符串?

javascript - node-express 错误 : express deprecated res. 发送(状态) : Use res. 发送状态(状态)改为

javascript - 尝试与服务 worker 一起显示离线页面,但即使在离线状态下,我的请求也会收到 200 响应

php - 使用 php ping 一个 ip,结果只有平均响应时间。

ios - 如何检查剪贴板中是否有内容

ios - 是否可以重新加载 UITableView 除了部分的特定行?

ios - 设置表头 View 的高度时出现奇怪的行为