【问题标题】:upload image using alamofire shows this issue (reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)使用 alamofire 上传图像显示此问题(原因:Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
【发布时间】:2018-10-22 21:09:08
【问题描述】:

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

@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” ); 服务器 = ( “微软-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 秒}

【问题讨论】:

  • HTTP 状态码 413 表示文件太大。检查服务器设置。
  • 好的,但是序列化兄弟呢?如果我尝试上传较小的图像,它会消失吗?
  • 同时我正在使用 UIImageJPEGRepresentation(image!, 0.7) 正如你看到的那样减小尺寸是不正确的吗?
  • 您使用的是 imageData 而不是 imageNewData。
  • 是的,我是因为后端开发人员要求我使用 base64

标签: swift4 alamofire


【解决方案1】:

将来要为谁搜索 我通过发出正常的 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: self)

                    }

                }

            }

        }

    }

}

【讨论】:

    猜你喜欢
    • 2020-11-11
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多