首页 > 解决方案 > 图片上传不起作用,iOS Swift Multipart

问题描述

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

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调用成功但图像未上传

请帮忙

标签: swiftalamofireimage-uploadingmultipartform-data

解决方案


推荐阅读