使用alamofire swift上传图片

时间:2017-01-21 21:26:01

标签: ios swift3

            self.manager!.request( RestApiManager.sharedInstance.baseURL+"rest/secure/add/"+self.id!+"/create?language="+lang, method: .post, parameters: requestDictionary, encoding: URLEncoding.httpBody,headers: headers
                ).responseObject(keyPath: "") { (response: DataResponse<User>) in

这就是我调用post方法的方法,但是我需要添加上传的图像文件。实体部分名为uploadFile。如何做到这一点?

entity.addPart("uploadFile", new FileBody(f100));


 let image_data = UIImagePNGRepresentation(imagetoadd.image!)

我想上传一个图片,参数标题也像用户名一样 - john和header,另外我想得到dataresponse类型用户(映射到)

我在swift 3中尝试这样做,它不起作用。

             self.manager!.upload(.post,RestApiManager.sharedInstance.baseURL+"rest/secure/stores/"+self.id!+"/create?language="+lang,
                                 // define your headers here
                multipartFormData: { multipartFormData in                      
                    // import image to request
                    if let imageData = UIImageJPEGRepresentation(image, 1) {
                        multipartFormData.appendBodyPart(data: image_data, name: "uploadFile", fileName: nameOfImage+".png", mimeType: "image/png")
                    }                
                    // import parameters
                    for (key, value) in parameters {
                        multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
                    }
            }, // you can customise Threshold if you wish. This is the alamofire's default value
                encodingMemoryThreshold: manager.MultipartFormDataEncodingMemoryThreshold,
                encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .Success(let upload, _, _):
                        upload.responseJSON { response in
                            debugPrint(response)
                        }
                    case .Failure(let encodingError):
                        print(encodingError)
                    }
            },headers: headers)
        }

在电话中使用trashold缺少参数。使用最新的alamofire库

1 个答案:

答案 0 :(得分:0)

func uploadImageInDatabase(image:UIImage)
{
    let imageData = UIImageJPEGRepresentation(image, 0.4)
    let url = "Your server url"
    let date = NSDate()
    let df = DateFormatter()
    df.dateFormat = "dd-mm-yy-hh-mm-ss"

    let imageName = df.string(from: date as Date)
    print(imageName)

     SVProgressHUD.show(withStatus: "Loading")

            Alamofire.upload(multipartFormData: { (multipartFormData) in
                 multipartFormData.append(imageData!, withName: "uploadedfile", fileName: "\(imageName).jpeg", mimeType: "image/jpeg")
                }, to: url) { (encodingResult) in


                    switch encodingResult {
                    case .success(let upload, _, _):

                        upload.responseJSON{
                            response in

                            if let data = response.result.value{
                                DispatchQueue.main.async {
                                    if(response.response?.statusCode == 200){
                                        //callback(true, JSON(data), JSON((response.response?.statusCode)!))
                                        let Data = data as! NSDictionary
                                        showAlert(message: Data["msg"]as! NSString)

                                        self.ImageURL = Data["data"]as! String
                                        print(self.ImageURL)
                                        SVProgressHUD.dismiss()

                                    } else{
                                        //callback(false, JSON(data), JSON((response.response?.statusCode)!))
                                        print(data)
                                        SVProgressHUD.dismiss()
                                    }
                                }
                            }

                        }

                    case .failure(let encodingError):
                        print(encodingError)
                        SVProgressHUD.dismiss()
                        DispatchQueue.main.async {
                            //callback(false,JSON(self.getStatusCodeString(408)),JSON(408))
                        }

    }


    }

}