无法在服务器中发送图像?

Posted

技术标签:

【中文标题】无法在服务器中发送图像?【英文标题】: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)")
                        
            )

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

【问题讨论】:

请查看以下问题的解决方案***.com/questions/41021802/… 对不起,我已经检查过这个,它对我不起作用!谢谢 【参考方案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 快乐编码我的朋友

以上是关于无法在服务器中发送图像?的主要内容,如果未能解决你的问题,请参考以下文章

无法将图像发送到 Go 服务器

在聊天应用程序中使用 Web 套接字 Api 无法正确显示图像

发送图像时出现 Python 套接字错误。无法解码字节/意外 EOF

无法在 setTimeout 中将流发送到服务器

在图像上添加按钮并将图像发送到服务器。从服务器获取图像后,在完全相同的位置显示按钮

无法从 python 服务器将图像上传到 Firebase 存储