开发者社区> 问答> 正文

使用Alamofire SWIFT使用MultipartFormData获取POST参数时出错

我无法上传图片到服务器获取。显示上载进度upload.uploadProgress但之后upload.responseJSON是nil错误。我已经尽力了,但解决不了这个问题。任何人都可以帮我。我做错什么了?当我调试它时{ (result) in switch result {结果无效。

邮递员钥匙:(参数)

ImageList -- for images
ProjectUnitID  -- 8568816

SWIFT代码:

if asset.type == .photo {

                let displayImage = asset.fullResolutionImage!
                images?.append(d)

                let token = UserDefaults.standard.string(forKey: "newToken")

                let image = displayImage
                let imgData = image.jpegData(compressionQuality: 0.2)!

                let parameters = ["ProjectUnitId": 8568816] 

                Alamofire.upload(multipartFormData: { multipartFormData in
                        multipartFormData.append(imgData, withName: "ImageList",fileName: "file.jpg", mimeType: "image/jpg")
                        for (key, value) in parameters {
                            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                            } 
                    },
                                 to:"http://AddProjectUnitImages", headers: [ "Authorization":"Bearer \(String(describing: token))"])
                { (result) in
                    switch result {
                    case .success(let upload, _, _):

                        upload.uploadProgress(closure: { (progress) in
                            print("Upload Progress: \(progress.fractionCompleted)")
                        })

                        upload.responseJSON { response in
                             print(response.result.value)
                        }

                    case .failure(let encodingError):
                        print(encodingError)
                    }
                }


                }

展开
收起
游客5akardh5cojhg 2019-12-23 18:30:47 2079 0
1 条回答
写回答
取消 提交回答
  • 尝试以下代码

    if asset.type == .photo {
       let displayImage = asset.fullResolutionImage!images?.append(d)
       guard let token = UserDefaults.standard.string(forKey: "newToken") else {
          print("invalid token") 
          return 
       }
       let image = displayImage
       let imgData = image.jpegData(compressionQuality: 0.2)!
       let parameters = ["ProjectUnitId": 8568816]                 
       Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(imgData, withName: "ImageList",fileName: "file.jpg", mimeType: "image/jpg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            } 
       },to:"http://AddProjectUnitImages", headers: [ "Authorization":"Bearer \(token)"]) { (result) in
       switch result {
       case .success(let upload, _, _):
           upload.uploadProgress(closure: { (progress) in
               print("Upload Progress: \(progress.fractionCompleted)")
            })
    
           upload.responseJSON { response in
              print(response.result.value)
           }
           case .failure(let encodingError):
              print(encodingError)
           }
         }
      }
    
    2019-12-23 18:31:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
OpenStack Swift 海量小文件优化之路 立即下载
From Java/Android to Swift iOS 立即下载
Swift在Airbnb的应用实践 立即下载