通过Almofire上传图像时失败

时间:2018-02-07 10:28:20

标签: ios swift3 alamofire multipartform-data

enter image description here

我正在上传多部分表单数据中的图片但是没有成功抛出错误。但是我试图在邮递员中成功了。我不知道我在哪里犯了错误。我附上了邮递员截图以上传响应。请检查它并且我使用Almofire进行响应。< / p>

 func uploadimages() {

    let url = ServiceUrl.Base + "ShopRegistration/ShopPicture"
    print("URL  === > \(url)")
    print(self.imgData?.count)
    var token :String = ""
    if let strToken = Preference.GetString(key: UserDefaultsKey.Token) {
        token = strToken
    }
    var RequestDist : NSDictionary = NSDictionary()
    RequestDist = ["ShopId": "\(Preference.GetInteger(key: UserDefaultsKey.ShopID))"]
        as NSDictionary;
    print(RequestDist)
    if(Reachability.isConnectedToNetwork())
    {
        Alamofire.upload(multipartFormData: { (multipartFormData) in
            if self.imgData != nil && (self.imgData?.count)! > 0 {
                for dataImg in (self.imgData)! {
                    //shopImage
                    multipartFormData.append(dataImg, withName: "shopImage", fileName: "uploaded_file.jpeg", mimeType: "image/jpeg")
                }
            }
            for (key, value) in  RequestDist {
                multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key as! String )
            }
            print("Request ===>>> /n \(multipartFormData.contentType)")
        }, to:url,headers :["authToken" : token])
        { (result) in
            switch result {
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (Progress) in
                    print("\n")
                    print(Progress.fractionCompleted)
                })
                upload.responseJSON { response in
                  if(response.result.isSuccess){
                    print("\n\n")
                    print("\(response.result.isSuccess)")
                    print("\n\n")
                    print(response.result)
                    print("\n\n")
                    print(response)
                    print("\n\n")
appDelegate.window?.rootViewController?.view.makeToast(message: "Images added sucessfully")
let datastring = NSString(data:response.data!, encoding:String.Encoding.utf8.rawValue) as String?
print("Response:::>>>> \(String(describing: datastring))")


if let intShopID : Int = Preference.GetInteger(key: UserDefaultsKey.ShopID) {
                            self.getShopImagesCall(intshopID: intShopID)
                        }

                    }else{
                        appDelegate.window?.rootViewController?.view.makeToast(message: AppMessage.getErrorInResponse)
                    }
                }
            case .failure(let encodingError):
                appDelegate.window?.rootViewController?.view.makeToast(message: AppMessage.getErrorInResponse)
                break
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

用于网络使用的JPEG图片 let imageData = UIImageJPEGRepresentation(img, 0.5)和身体 multipartFormData.append(dataImg, withName: "shopImage", fileName: "uploaded_file.jpeg", mimeType: "image/jpeg")

表示PNG图片 let image = UIImagePNGRepresentation(pickedImage)和身体 multipartFormData.append(dataImg, withName: "shopImage", fileName: "uploaded_file.png", mimeType: "image/png")