【问题标题】:alamofire multipartformdata use urlrequestalamofire multipartformdata 使用 urlrequest
【发布时间】:2017-12-17 15:25:20
【问题描述】:

我想使用Alamo fire multipart form数据使用请求,例如我使用upload API

let profile = self.photoView.imageView?.image

    let parameters : [String:String] = [
        "homePageUrl": webURLField.text!,
        "nickName": nickNameField.text!,
        "selfIntro": introField.text!,
        ]

    let uri = Constants.APIURL.changeProfile
    let fileName = "\(nickNameField.text!).jpg"

    Alamofire.upload(multipartFormData: { (multipartFormData) in
         if let imageData = UIImageJPEGRepresentation(profile!, 1.0) {
            multipartFormData.append(imageData, withName: "profile", fileName: fileName, mimeType: "image/jpg")
        }

        for ( key, value ) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: key)
        }

    }, usingThreshold: UInt64.init(), to: uri, method: .patch, headers: Constants.VyrlAPIConstants.getHeader(), encodingCompletion:
        {
            encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                 })

                upload.responseString { response in
                    if ((response.response?.statusCode)! == 200){
                        self.navigationController?.popViewController(animated: true)                            
                    }
                }
            case .failure(let encodingError):
                print(encodingError.localizedDescription)
            }
    })

此代码从服务器发送 400 响应 日志是。

补丁“http://blablabla.com:8080/users/profile”:

内容类型:multipart/form-data; 边界=alamofire.boundary.53a2e440bad1fabd X-设备:ios X-APP 版本:1.0.0 接受语言:ko-kr 400 'http://blablabla.com:8080/users/profile' [0.0976 秒]: X-应用程序上下文:应用程序:开发连接:关闭 传输编码:身份日期:2017 年 7 月 13 日星期四 01:57:41 GMT

服务器想要接收 http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is.. 服务器日志是..(成功200代码)

curl -X PATCH --header 'Content-Type: multipart/form-data' --header '接受:/' --header 'X-APP-Version: 1.0.0' --header 'X-Device: ios' --header '接受语言:ko-KR' {"type":"formData"} 'http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is ..'

当然,我想附上 ?nicName=abcd 使用 multiformpart 数据

如何使用alamofire代码?

【问题讨论】:

    标签: ios swift upload alamofire patch


    【解决方案1】:

    请试试这个,它对我上传图片有用

     var parameters = [String:AnyObject]()
    
    
        let profile = self.photoView.imageView?.image
    
        parameters = ["homePageUrl": webURLField.text as AnyObject,
                      "nickName": nickNameField.text as AnyObject,
                      "selfIntro": introField.text as AnyObject]
    
    
        let imgData = UIImageJPEGRepresentation(profile!, 0.2)!
    
        let uri = Constants.APIURL.changeProfile
        let fileName = "\(nickNameField.text!).jpg"
    
    
        Alamofire.upload(multipartFormData:{ multipartFormData in
            multipartFormData.append(imgData, withName: "profile",fileName: fileName, mimeType: "image/jpg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        },
                         usingThreshold:UInt64.init(),
                         to:uri,
                         method:.post,
                         headers:Constants.VyrlAPIConstants.getHeader(),
                         encodingCompletion: { encodingResult in
                            switch encodingResult {
                            case .success(let upload, _, _):
                                upload.responseJSON { response in
                                     print(response.result.value)
                                }
                            case .failure(let encodingError):
                                print(encodingError)
                            }
        })
    

    【讨论】:

    • 谢谢你 :) 但是,我想要参数来查询字符串,而不是 multiformdata
    【解决方案2】:

    如果不起作用,则使用这样的参数制作 url。

     Alamofire.upload(multipartFormData:{ multipartFormData in
            multipartFormData.append(imgData, withName: "profile",fileName: fileName, mimeType: "image/jpg")},
                         usingThreshold:UInt64.init(),
                         to:uri + "?" +nickNameField.text! +"?"+ webURLField.text!+"?"+introField.text!,
                         method:.post,
                         headers:Constants.VyrlAPIConstants.getHeader(),
                         encodingCompletion: { encodingResult in
                            switch encodingResult {
                            case .success(let upload, _, _):
                                upload.responseJSON { response in
                                     print(response.result.value)
                                }
                            case .failure(let encodingError):
                                print(encodingError)
                            }
        })
    

    【讨论】:

      【解决方案3】:

      在 Swift 3 中 Alamofire 4

      这是一个演示代码供参考

      Alamofire.upload(multipartFormData: { (multipartFormData) in
              multipartFormData.append(UIImageJPEGRepresentation(self.Imgprofile.image!, 1)!, withName: "Prescription", fileName: "Profile_Image.jpeg", mimeType: "image/jpeg")
          }, to:" Your URL here ")
          { (result) in
              switch result {
              case .success(let upload, _, _):
                  print(result)
      
                  upload.uploadProgress(closure: { (progress) in
                      print(progress)
                  })
      
                  upload.responseJSON { response in
                      //print response.result
                      print(response);
                  }
      
              case .failure(let encodingError):
                  print(encodingError);
              }
          }
      }
      

      希望这应该工作

      谢谢

      【讨论】:

        猜你喜欢
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        • 2019-03-04
        • 2017-01-30
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 1970-01-01
        相关资源
        最近更新 更多