多部分图像Alamofire 4 swift 3

多部分图像Alamofire 4 swift 3,swift,image,upload,alamofire,multipart,Swift,Image,Upload,Alamofire,Multipart,当我尝试将图像更新到数据库时,我遇到了一个大问题 我必须上传一张图片和一些字符串值 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

当我尝试将图像更新到数据库时,我遇到了一个大问题

我必须上传一张图片和一些字符串值

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imageCropped = selectedImage
            self.sendImageWithMultipart()
            imagePicker.dismiss(animated: true, completion: { _ in })
        }
    }

func sendImageWithMultipart() {        
        Alamofire.upload(multipartFormData: {
            multipartFormData in
            multipartFormData.append("\(HGYGjihf746fg743g)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"auth_token")
            multipartFormData.append("\(0)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os")
            multipartFormData.append("\(10.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"os_version")
            multipartFormData.append("\(3.0.3)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"app_version")

            if let photo = self.imageCropped, let jpegImage = UIImageJPEGRepresentation(photo, 50.0) {
                print(jpegImage)
                multipartFormData.append(jpegImage, withName: "profile_photo", mimeType: "image/*")
            }
        }, to: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)", method:.post, headers:["Content-Type":"multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__"], encodingCompletion: {
            encodingResult in
            switch encodingResult {
            case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
                upload.validate().responseJSON {
                    response in
                    if response.result.isFailure {
                        debugPrint(response)
                    } else {
                        debugPrint(response)
                    }
                }
            case .failure(let encodingError):
                NSLog((encodingError as NSError).localizedDescription)
            }
        })
    }
但结果是:

[Request]: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)"
[Response]: <NSHTTPURLResponse: 0x17023c0e0> { URL: "\(Constants.GENERAL_ADDRESS_2)\(Constants.API_MODIFYUSER_PROFILE_PHOTO)" } { status code: 500, headers {
    "Cache-Control" = "no-cache, private";
    Connection = close;
    "Content-Length" = 5545;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Thu, 10 Nov 2016 10:49:00 GMT";
    Server = "Apache/2.4.18 (Amazon) OpenSSL/1.0.1k-fips PHP/5.6.21";
    "X-Powered-By" = "PHP/5.6.21";
} }
[Data]: 5545 bytes
[Result]: FAILURE: responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500))
[Timeline]: Timeline: { "Request Start Time": 500467740.667, "Initial Response Time": 500467740.793, "Request Completed Time": 500467743.733, "Serialization Completed Time": 500467743.733, "Latency": 0.126 secs, "Request Duration": 3.066 secs, "Serialization Duration": 0.000 secs, "Total Duration": 3.066 secs
[请求]:“\(常数.通用地址\u 2)\(常数.API\u修改用户\u配置文件\u照片)”
[回复]:{URL:\(Constants.GENERAL\u ADDRESS\u 2)\(Constants.API\u MODIFYUSER\u PROFILE\u PHOTO)}{状态代码:500,标题{
“缓存控制”=“无缓存,专用”;
连接=关闭;
“内容长度”=5545;
“内容类型”=“文本/html;字符集=UTF-8”;
日期=“2016年11月10日星期四10:49:00 GMT”;
Server=“Apache/2.4.18(Amazon)OpenSSL/1.0.1k-fips PHP/5.6.21”;
“X-Powered-By”=“PHP/5.6.21”;
} }
[数据]:5545字节
[结果]:失败:responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500))
[时间线]:时间线:{“请求开始时间”:500467740.667,“初始响应时间”:500467740.793,“请求完成时间”:500467743.733,“序列化完成时间”:500467743.733,“延迟”:0.126秒,“请求持续时间”:3.066秒,“序列化持续时间”:0.000秒,“总持续时间”:3.066秒
问题是我如何转换图像、内容类型或什么


谢谢

这可能会解决您的问题:

let endpoint = "https://api.blah/images/" + id + "/documents"

let imageData = UIImagePNGRepresentation(image)

Alamofire.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(type.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"type")
                multipartFormData.append(imageData!, withName: "file.png", mimeType: "image/png")
        }, to: endpoint, method:.post, headers:headers, encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
                upload.validate().responseJSON { (response) in
                    if response.result.isFailure {
                        debugPrint(response)
                    } else {
                        debugPrint(response)
                    }
                }
            case .failure(let encodingError):
                NSLog((encodingError as NSError).localizedDescription)
            }
        })

似乎到达了我的服务器,但在我的情况下,我返回了400(可能未通过tv4验证)。

这可能会解决您的问题:

let endpoint = "https://api.blah/images/" + id + "/documents"

let imageData = UIImagePNGRepresentation(image)

Alamofire.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(type.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"type")
                multipartFormData.append(imageData!, withName: "file.png", mimeType: "image/png")
        }, to: endpoint, method:.post, headers:headers, encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
                upload.validate().responseJSON { (response) in
                    if response.result.isFailure {
                        debugPrint(response)
                    } else {
                        debugPrint(response)
                    }
                }
            case .failure(let encodingError):
                NSLog((encodingError as NSError).localizedDescription)
            }
        })

似乎到达了我的服务器,但在我的情况下,我返回了400(可能未通过tv4验证)。

是否显式指定mime类型:
multipartFormData.append(jpegImage,名称为:“profile\u photo.jpeg”,mimeType:“image/jpeg”)
你能解决吗?我没能解决这个问题。我也尝试过指定mime类型,但没有明确指定mime类型的方法:
multipartFormData.append(jpegImage,名称为:“profile\u photo.jpeg”,mime类型:“image/jpeg”)
你能解决问题吗?我没能解决问题。我也尝试过指定mimetype,但没有什么看起来像是答案,但如果你有相关问题,请作为新问题提问。问题材料大部分不会在此处显示,显然是问题的材料通常会被模式删除但是我必须使用图片?而不是图片的URL?大部分看起来像是一个答案,但是如果你有相关的问题,请作为一个新的问题提问。问题材料大部分不会在这里看到,显然是问题的材料通常会被版主删除。但是我必须使用图片?而不是图片的URL?