【问题标题】:Multipart Image Alamofire 4 swift 3多部分图像 Alamofire 4 swift 3
【发布时间】:2016-11-10 11:00:06
【问题描述】:

当我尝试将图像更新到我的 Db 时,我遇到了一个大问题。

我必须上传一张图片,以及一些字符串值。

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

问题是我如何转换图像或内容类型或什么?

谢谢

【问题讨论】:

  • 显式指定mime类型怎么样:multipartFormData.append(jpegImage, withName: "profile_photo.jpeg", mimeType: "image/jpeg")
  • 你解决了吗?
  • 我无法解决问题。而且我也尝试过指定 mimetype,但没有

标签: swift image upload alamofire multipart


【解决方案1】:

这可能会解决您的问题:

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 验证)。

【讨论】:

  • 大部分内容看起来像是一个答案,但如果您有相关问题,请将其作为一个新问题提出。问题材料大多不会在这里看到,明显是问题的材料通常会被版主删除。
  • 但我必须使用图像?不是图像的 URL?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多