【问题标题】:alamofire upload images with json parametersalamofire上传带json参数的图片
【发布时间】:2017-04-22 11:05:02
【问题描述】:

我是 swift 的新手 我想上传 images(从 1 到 4 - 取决于用户)和 video 文件(如果用户愿意)到我的服务器,并在上传时发送其他参数(如文字等) 在 android 中,我正在使用改造,它允许我使用 @partmap 来上传我的嵌套 Json 对象。 现在在 iOS 我正在使用 Alamofire 但我不能 找到这个功能来发送带有多部分的 json 我该怎么做? 我不想要 base 64 图像我已经试过了 我的文本内容是嵌套的 显然我想在上传数据时发送json参数 例如:

image 1 = image
post->content = "some text"

【问题讨论】:

  • @SaqibOmer 我的参数是嵌套的,所以我不知道如何使用提到的解决方案。你能帮忙吗?
  • 首先你需要做一个多部分请求。您可以在请求中添加参数。您可以使用 NSDictionary 创建嵌套参数。另外,如果您可以编辑您的问题添加一些代码,也许我可以回答。
  • @SaqibOmer 我使用 Alamofire 作为网络库,它提供了上传多部分数据的上传方法,但在这种方法中,我只能将文本数据作为键值对发送,但我的数据是嵌套的,服务器端是分开的这个数据
  • 检查我的答案。

标签: json swift alamofire image-uploading multipartform-data


【解决方案1】:

正如我在 cmets 中提到的,您需要一个多部分请求。

// Image file which you want to upload.
guard let image = UIImage(named: "YourImage") else { return }


                let imgRep : NSBitmapImageRep = image.representations.first as! NSBitmapImageRep

                let imgData : Data = imgRep.representation(using: .PNG, properties: [:])!


                let cameraFileName    = "some_random_name_img.png"

                let fileUrl = URL(fileURLWithPath: cameraFileName, isDirectory: true)
                do {

                    try imgData.write(to: fileUrl, options: NSData.WritingOptions.atomic)

                    DispatchQueue.main.async {
                        // Stop Camera Session if you are capturing image from camera
                        self.cameraSession.stopRunning()

                        let someParam   = ""
                        let someParam2  = ""


                        let date = Date()
                        let dateString = "\(date)"



                        Alamofire.upload(multipartFormData: { (multipartFormData) in

                        // Add parameters in request 

         multipartFormData.append(cameraFileName.data(using: .utf8)!, withName: "name")

         multipartFormData.append(dateString.data(using: .utf8)!, withName: "startDatetime")

         multipartFormData.append(someParam.data(using: .utf8)!, withName: "userId")

         multipartFormData.append(someParam2.data(using: .utf8)!, withName: "phoneServiceId")

         multipartFormData.append(fileUrl, withName: "file")

                        }, to:"Your_Api_Url.com") // POST Url
                        { (result) in
                            switch result {
                            case .success(let upload, _ , _):

                                upload.uploadProgress(closure: { (progress) in

                                    print("uploding")
                                })

                                upload.responseJSON { response in
                                    print(response)
                                    print("done")






                                }

                            case .failure(let encodingError):
                                print("failed")
                                print(encodingError)

                            }
                        }

                    }

                }
                catch let error as NSError {
                    print("Ooops! Something went wrong: \(error)")
                }

【讨论】:

  • 我将我的 json 转换为字符串,然后将其作为多部分数据添加到 alamofire 请求中,它运行良好,感谢您的关注
猜你喜欢
  • 2019-08-18
  • 2019-03-16
  • 2023-03-22
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2015-07-11
  • 2019-05-18
相关资源
最近更新 更多