【问题标题】:Extract Response message from Alamofire upload Image从 Alamofire 上传图像中提取响应消息
【发布时间】:2017-04-18 06:15:02
【问题描述】:

我需要从发布请求中提取 json 响应,以便使用 Alamofire 将图像上传到服务器并通过以下代码将图像作为 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 值作为字符串?

【问题讨论】:

    标签: ios swift response alamofire image-uploading


    【解决方案1】:

    找到从响应中获取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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2011-02-28
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      相关资源
      最近更新 更多