【问题标题】:Cannot send image in server?无法在服务器中发送图像?
【发布时间】:2017-04-02 04:54:19
【问题描述】:

我无法使用库 alamofire 将图像文件发送到服务器。我在动态发送图像文件时遇到问题。文件名显示在服务器中,但图像为空白。如何解决?

图片代码

 let parameters = [
                "name": name_txt.text! as String,
                "address": address_txt.text! as String,
                "district": website_txt!.text! as String,
                "country": establishment_txt.text! as String,

            ]

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

                for (key, value) in parameters {
                    multipartFormData.append((value.data(using: .utf8))!, withName: key)
                }}, to: "http://www.myeducationhunt.com/api/v1/schools", method: .post, headers: ["Authorization": "auth_token"],
                    encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .success(let upload, _, _):
                            upload.response { [weak self] response in
                                guard self != nil else {
                                    return
                                }
                                debugPrint(response)
                            }
                        case .failure(let encodingError):
                            print("errorss:\(encodingError)")
                        }
            })

我能够接收图像文件名,但图像变成空白。这怎么能 解决了吗?

【问题讨论】:

标签: ios image swift3 alamofire


【解决方案1】:

你可以试试这个

let parameters = [

               "name": name_txt.text! as String,

               "address": address_txt.text! as String,

               "district": website_txt!.text! as String,

               "country": establishment_txt.text! as String



           ]





        Alamofire.upload(multipartFormData: { multipartFormData in

               if let imageData = UIImageJPEGRepresentation(self.logo_holder.image!, 0.5) {

                   multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "file/jpeg")

                   print("imgdatas",imageData)

               }



               for (key, value) in parameters {

                   multipartFormData.append((value.data(using: .utf8))!, withName: key)

               }}, to: "http://www.myeducationhunt.com/api/v1/schools", method: .post, headers: ["Authorization": "auth_token"],

                   encodingCompletion: { encodingResult in

                       switch encodingResult {

                       case .success(let upload, _, _):

                           upload.response { [weak self] response in

                               guard self != nil else {

                                   return

                               }

                               debugPrint(response)

                           }

                       case .failure(let encodingError):

                           print("errorss:\(encodingError)")

                       }

           })

【讨论】:

  • 是的!它有效,我尝试了很多方法感谢您的帮助@ghimire
  • 快乐编码我的朋友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2015-07-27
  • 2012-08-31
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多