使用alamofire上传图片显示了此问题(原因:Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

时间:2018-10-22 21:09:09

标签: swift4 alamofire

我正在尝试将我的代码上传到服务器上的图像

@IBAction func startUploadClicked(_ sender: UIButton) {

    // get image form uiimageview 
    let image = theImage.image

    // and here is the api example provided by my backend developer 

   //{
   //"Id":11,
   //"FileName":"Hydrangeas.jpg",
   //"ImageData":"base64string"
   //}

    let imageData = convertImageToBase64(image: image!)
    let imageNewData = UIImageJPEGRepresentation(image!, 0.7)
    let Id = "\(12)"
    let FileName = "image.jpg"




    let headers = [
        "SecurityToken": UserDefaults.standard.string(forKey: "SecurityToken")!,
        "api_key": "Ml3BHS17tJ89Y3Tf4QC3",
        "Content-Type":"application/json"
    ]


    let parameters = [
        "Id":"\(12)",
        "FileName":"image.jpg",
        "ImageData":"\(imageData)"
        ]


    Alamofire.upload(
        multipartFormData: { multipartFormData in

            for (key, value) in parameters {
                multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName: key)
            }

    },
        to: "https://serverName/AddPhoto", headers: headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in

                    debugPrint(response)
                }
            case .failure(let encodingError):

                print(encodingError)
            }
    }
    )
}

这是运行代码后在命令中显示的内容

  

[请求]:POST https://serverName/AddPhoto   [响应]:{URL:https://serverName/AddPhoto} {状态代码:413,标题{       “内容长度” =(           0       );       日期=(           “ 2018年10月22日星期一20:52:43 GMT”       );       服务器=(           “ Microsoft-IIS / 8.5”       );       “ X-Powered-By” =(           “ ASP.NET”       );   }}   [数据]:0字节   [结果]:失败:responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)   [时间线]:时间线:{“请求开始时间”:561934363.529,“初始响应时间”:561934363.836,“请求完成时间”:561934364.234,“序列化完成时间”:561934364.241,“等待时间”:0.307秒,“请求持续时间” :0.705秒,“序列化持续时间”:0.006秒,“总持续时间”:0.711秒}

1 个答案:

答案 0 :(得分:0)

将来想为其搜索的人 我通过发出正常的Alamofire请求而不是使用Alamofire.upload解决了它 因为我已经在这里将新图片转换为字符串

@IBAction func startUploadClicked(_ sender: UIButton) {


    let image = theImage.image
    let imageData = convertImageToBase64(image: image!)


    let headers = [
        "SecurityToken": UserDefaults.standard.string(forKey: "SecurityToken")!,
        "api_key": "Ml3BHS17tJ89Y3Tf4QC",
        "Content-Type":"application/json"
    ]


    let parameters = [
        "Id":"\(balaghID)",
        "FileName":"image.jpg",
        "ImageData":"\(imageData)"
        ]

    Alamofire.request("https://serverName/addPhoto", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: Constant.Header).responseJSON { response in



        if let JSON = response.result.value as? [String:Any] {

            if let ActionResult = JSON["ActionResult"] as? [String:Any]{

                if let Errcode = ActionResult["Errcode"] as? NSNumber {

                    if Errcode != NSNumber(value: 0) {

                        Helper.showAlert("bad operation", message: "error \(ActionResult["ErrDescription"]!)", VC: self)

                    } else {

                        Helper.showAlert("Great you make it", message: "

(ActionResult [“ ErrDescription”]!)“,VC:自我)

                    }

                }

            }

        }

    }

}