【问题标题】:How to append MultipartFormData along with other post parameters using Alamofire and session manager? [duplicate]如何使用 Alamofire 和会话管理器附加 MultipartFormData 以及其他帖子参数? [复制]
【发布时间】:2019-02-14 08:34:52
【问题描述】:

目前我使用:

func post(_ url: URL, parameters: [String: Any]?, image: UIImage?, headers: [String: String]?, success: @escaping SuccessHandler, failure: @escaping ErrorHandler) {
    manager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON { [weak self] response in
        //do something with a response
    }
}

如何将图像附加到请求中?

【问题讨论】:

  • 你应该使用 alamofire 的多部分请求
  • 如何使用多部分请求?你能分享一个例子吗?
  • 请查看我的回答

标签: ios swift alamofire


【解决方案1】:

这是我的答案

 Alamofire.upload(multipartFormData: { multipartFormData in

            //Single image to pass here
            if image != nil{
                if let imageData = UIImageJPEGRepresentation(image!, 0.8) {
                    multipartFormData.append(imageData, withName: "Your key here", fileName: "\(UUID().uuidString).jpeg", mimeType: "image/jpeg")
                }
            }

            //Here array of UIImages
            if images.count != 0{
                for img in images{
                    if let imageData = UIImageJPEGRepresentation(img, 0.8) {
                        multipartFormData.append(imageData, withName: "Your key here", fileName: "\(UUID().uuidString).jpeg", mimeType: "image/jpeg")
                    }
                }
            }


            //params are your other post parameters
            if let parm = params{
                for (key, value) in parm {
                    multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
                }
            }


         }, to: "Your url here" , method: .post, headers: headers,
            encodingCompletion: { encodingResult in


                switch encodingResult {
                case .success(let upload, _, _):
                    //Success response
                case .failure(let encodingError):
                    //Failure response

                }
         })

【讨论】:

    【解决方案2】:

    你应该考虑使用上传方式:

    manager.upload(multipartFormData: { (multipartFormData) in
    
            for (key,value) in data{
                multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
            }
    
            if let file = self.attachedFile{
                if let f = file.getFile()
                {
                    multipartFormData.append(f, withName: "some_name", fileName: "attachement.pdf", mimeType: "application/pdf")
                }
            }
    
        }, to:"Some URL",
           method: .post,
           encodingCompletion: { encodingResult in 
    
    
                switch encodingResult {
                case .success(let upload, _, _):
                    //Success response
                case .failure(let encodingError):
                    //Failure response
    
                }
         })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      相关资源
      最近更新 更多