图片上传不起作用,iOS Swift Multipart

时间:2018-07-18 11:43:25

标签: swift alamofire image-uploading multipartform-data

我必须将图像上传到服务器,但是我的行为异常,

if let imgdata = UIImageJPEGRepresentation(#imageLiteral(resourceName: "vishal"), 0.2) {

        let headers: HTTPHeaders = [
            "token": authToken,
            "Content-type": "multipart/form-data"
        ]

        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imgdata, withName: "image", fileName: "image.png", mimeType: "image/png")
        }, usingThreshold: UInt64.init(), to: url, method: .patch, headers: headers) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    print("Succesfully uploaded")
                    if let _ = response.error{
                        return
                    }
                    if let value = response.result.value {
                        let json = JSON(value)
                        print(json)
                    }
                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")
            }
        }
    }

在新项目中,此代码可以正常工作并将图像上传到服务器,但是在我的旧项目中,对于相同的代码,api调用成功,但是图像未上传

请帮助

0 个答案:

没有答案