Alamofire上传带参数的照片

时间:2018-04-25 08:53:54

标签: ios swift file-upload alamofire multipartform-data

我想用其他参数上传照片我使用imagePickerController

internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    imagePicked.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    dismiss(animated:true, completion: nil)

}

我使用此功能来处理服务但无法正常工作

public static func addCar(photo:UIImage, api_token: String, plate_number: String, type: Int, enddate: String, completion: @escaping (_ error: Error?,_ success: Bool)->Void){

        let parameters = ["api_token": api_token,"plate_number":plate_number,"type": type, "enddate": enddate] as [String : Any]

        let AddCarUrl = APIHelper.BASE_URL + APIHelper.API.ADD_CAR.rawValue

        var jsonData : Data!
        do {
            jsonData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
            // here "jsonData" is the dictionary encoded in JSON data

            let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
            // here "decoded" is of type `Any`, decoded from JSON data

            // you can now cast it with the right type
            if let dictFromJSON = decoded as? [String:String] {
                // use dictFromJSON
                print(dictFromJSON)
            }
        } catch {
            print(error.localizedDescription)
        }

        Alamofire.upload(multipartFormData: { (form: MultipartFormData) in

            form.append(UIImageJPEGRepresentation(photo, 0.5)!, withName: "photo", fileName: "photo", mimeType: "photo/jpeg")
            form.append(jsonData, withName: "data")

            print(jsonData)

        }, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold,
           to: AddCarUrl,
           method: .post,
           headers: nil) { (result: SessionManager.MultipartFormDataEncodingResult) in

            switch result {

            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    print(progress)
                }).responseJSON(completionHandler: { (response) in

                    switch response.result {
                    case .success(let value):
                        let json = JSON(value)
                        print(json)

                    case .failure(let error):
                        print("Error: ",error)
                    }
                })

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

我得到一些数据丢失的错误

<NSProgress: 0x60400012cd00> : Parent: 0x0 / Fraction completed: 0.0165 / Completed: 8192 of 497021  

:父:0x0 /分数完成:0.4615 /已完成:229376 of 497021
 :父:0x0 /分数完成:0.6758 /已完成:335872 of 497021
 :父:0x0 /分数完成:0.9065 /已完成:4507060 of 497021
 :父:0x0 /分数完成:1.0000 /完成:497021 497021
{   &#34;代码&#34; :&#34; 404&#34;,   &#34;错误&#34; :{     &#34;类型&#34; :[       &#34;类型字段是必需的。&#34;     ]     &#34;图像&#34; :[       &#34;图像字段是必需的。&#34;     ]     &#34; plate_number&#34; :[       &#34;板号字段是必需的。&#34;     ]   } }

为什么我会收到此错误,虽然我上传图片和plate_number并输入。

任何人都可以帮助我。

提前致谢

0 个答案:

没有答案