在swift4中的alamofire中的多部分表单数据请求?

问题描述 投票:0回答:2

您好我是swift4的新手,并尝试用swift4语言的alamofire库调用多部分表单数据请求?以下是我的代码

Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append("1422".data(using: String.Encoding.utf8)!, withName: "u_id")
            multipartFormData.append("How???".data(using: String.Encoding.utf8)!, withName: "firstname")
            multipartFormData.append("We are woking on it!!".data(using: String.Encoding.utf8)!, withName: "description")
        }, to: "URL_TOcall", encodingCompletion: { result in
            print("result  = \(result)")
            switch result {
            case .success(request: let request, streamingFromDisk: false, streamFileURL: nil):
                //Success code
                break

            case .failure:
                print("failed api calling")
                break


            case .success(let request, true, _):
                //Code here
                break

            case .success(let request, _, _):
                // Code here
                break
            }


        })
}
ios iphone swift4 alamofire
2个回答
0
投票

如果您因为尚未上传图像数据而面临问题

Alamofire.upload(multipartFormData: { multipartFormData in
            // MISSING THIS LINE
            if let imageData = UIImagePNGRepresentation(yourimage) {
                multipartFormData.append(imageData, withName: "", fileName: "filename\(Date().timeIntervalSince1970)")
            }

            multipartFormData.append("1422".data(using: String.Encoding.utf8)!, withName: "u_id")
            multipartFormData.append("How???".data(using: String.Encoding.utf8)!, withName: "firstname")
            multipartFormData.append("We are woking on it!!".data(using: String.Encoding.utf8)!, withName: "description")
    }, to: "URL_TOcall", encodingCompletion: { result in
        print("result  = \(result)")
         switch result {
            case .success(let request, _,  _):
                print(request)
                request.responseJSON(completionHandler: { (resonse) in
                    print(resonse)

                })


            case .failure( let error):
                print(error)
                break
            }
       }

    })
}

0
投票

我已经解决了我的问题,这是太愚蠢和小事,我没有做但是在添加allowLossyConversion之后:false我的字符串转换只是做了诀窍。

{
 //corrected indentation for code

 Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append("1422".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "u_id")
        multipartFormData.append("How???".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "firstname")
        multipartFormData.append("We are woking on it!!".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "description")
    }, to: "URL_TOcall", encodingCompletion: { result in
        print("result  = \(result)")
        switch result {
        case .success(request: let request, streamingFromDisk: false, streamFileURL: nil):
            //Success code
            break

        case .failure:
            print("failed api calling")
            break


        case .success(let request, true, _):
            //Code here
            break

        case .success(let request, _, _):
            // Code here
            break
        }
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.