通过Swift 3和Alamofire 4上传带有JSON和自定义标题的照片/文件iOS |迅速

我需要使用 Image文件JSON调用Multipart请求.

我试过这个,但仍然得到错误.

// define parameters
  let parameters = [
    "hometown": "yalikavak","living": "istanbul"
  ]

Alamofire.upload(multipartFormData: { multipartFormData in
    if let imageData = UIImageJPEGRepresentation(image,1) {
      multipartFormData.append(imageData,withName: "file",fileName: "file.png",mimeType: "image/png")
    }

    for (key,value) in parameters {
      multipartFormData.append((value?.data(using: .utf8))!,withName: key)
    }},to: "upload_url",method: .post,headers: ["Authorization": "auth_token"],encodingCompletion: { encodingResult in
          switch encodingResult {
          case .success(let upload,_,_):
            upload.response { [weak self] response in
              guard let strongSelf = self else {
                return
              }
              debugPrint(response)
            }
          case .failure(let encodingError):
            print("error:\(encodingError)")
          }
  })
}

如何发送JSON?

在单个请求中尝试使用此代码进行多个上传图像,此代码已经可用.
// For Pass Valid Parameters & number of Images in Array in Image Upload Function
     var dicImgData : NSMutableDictionary? = NSMutableDictionary()

     if let img = UIImage(named: "Your Image") {
         if let data:Data = UIImagePNGRepresentation(img) {
             var imageData : NSData = data
             dicImgData! .setobject(imageData,forKey: "data" as NScopying)
             dicImgData! .setobject("file",forKey: "name" as NScopying)
             dicImgData! .setobject("file.png",forKey: "fileName" as NScopying)
             dicImgData! .setobject("image/png",forKey: "type" as NScopying)

             let dicParameter = [
                 "hometown": "yalikavak","living": "istanbul"
             ]

             self.uploadImage(url: "Your URL",Parameter: dicParameter,Images: [dicImgData])
         }
    }

上传功能

func uploadImage (url: String,Parameter param : NSDictionary,Images arrImage: NSArray) -> Void
    {
        var requestURL : String! = url
        let headers = [
            "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==","Accept": "application/json",]

        print("---------------------")
        print("Request URL :- \(requestURL)")
        print("---------------------")

        Alamofire.upload(multipartFormData: { (data) in

            for (key,value) in param {
                data.append((value as! String).data(using: .utf8)!,withName: key as! String)
            }

            for imageInfo in arrImage
            {
                var dicInfo : NSDictionary! = imageInfo as! NSDictionary
                data.append(dicInfo["data"] as! Data,withName: dicInfo["name"] as! String,fileName: dicInfo["fileName"] as! String,mimeType: dicInfo["type"] as! String)
                dicInfo = nil
            }

        },to: requestURL,headers:nil,encodingCompletion: { (encodeResult) in
            switch encodeResult {
            case .success(let upload,_):

                upload.responseJSON(completionHandler: { (response) in

                    switch response.result
                    {
                    case .success(let responseJSON):
                        guard let dicResponse = responseJSON as? NSDictionary else{
                            return
                        }

                        print("Response : \((dicResponse))")

                    case .failure(let error):

                        print(error)

                        break
                    }
                })
            case .failure(let error):
                print(error)
                break
            }
        })
    }

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...