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

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

概述我需要使用 Image文件和 JSON调用Multipart请求. 我试过这个,但仍然得到错误. // define parameters let parameters = [ "hometown": "yalikavak", "living": "istanbul" ]Alamofire.upload(multipartFormData: { multipartFor 我需要使用 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            }        })    }
总结

以上是内存溢出为你收集整理的通过Swift 3和Alamofire 4上传带有JSON和自定义标题的照片/文件iOS |迅速全部内容,希望文章能够帮你解决通过Swift 3和Alamofire 4上传带有JSON和自定义标题的照片/文件iOS |迅速所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1030696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存