使用Alamofire在swift中使用multipart上传图像和参数

时间:2015-11-06 14:11:33

标签: swift

我是swift的新手。我使用Alamofire上传图像和参数与multipart但我的代码不起作用。请帮帮我,我做错了什么。

Alamofire.upload(.POST, kServerURLForUploadDocument,headers: headerDict, multipartFormData: { multipartFormData in

        if let _image = image
        {
            if let imageData = UIImageJPEGRepresentation(_image, 1.0)
            {
                multipartFormData.appendBodyPart(data: imageData, name:kDocument_image)
                multipartFormData.appendBodyPart(data: self.registrationNumber.dataUsingEncoding(NSUTF8StringEncoding)!, name: kRegistration_number)
                multipartFormData.appendBodyPart(data: "168".dataUsingEncoding(NSUTF8StringEncoding)!, name: kUser_id)
                multipartFormData.appendBodyPart(data: "jpg".dataUsingEncoding(NSUTF8StringEncoding)!, name: kType)
                multipartFormData.appendBodyPart(data: "test".dataUsingEncoding(NSUTF8StringEncoding)!, name: kComment)
            }
        }

        },
        encodingCompletion: { encodingResult in
            loader.hide(true)
            switch encodingResult
            {
            case .Success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                    print("response::::::::",response)

                }
            case .Failure(let encodingError):
                print("encodingError:::::::",encodingError)
            }
        }
    )

响应消息是: response :::::::: FAILURE:Error Domain = NSCocoaErrorDomain Code = 3840“字符0周围的值无效。” UserInfo = {NSDebugDescription =字符0周围的值无效。}

1 个答案:

答案 0 :(得分:0)

这也是服务器端的一些问题,我只是更改添加图像数据线,它工作正常.Server是非JSON格式的返回数据

multipartFormData.appendBodyPart(data: self.registrationNumber.dataUsingEncoding(NSUTF8StringEncoding)!, name: kRegistration_number)