2017-04-02 25 views
0

我無法使用庫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)") 
         } 
      }) 

我能夠接收圖像的文件名,但轉眼圖像可以blank.How這 解決?

+0

請檢查下面的問題的解決http://stackoverflow.com/questions/41021802/alamofire-4-and-swift-3-image-upload與其他參數 –

+0

對不起,我已經檢查過這個已經不適合我!謝謝 –

回答

0

你可以試試這個

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)") 

         } 

      }) 
+0

是啊!它的作品,我嘗試了很多方法謝謝你的幫助@ghimire –

+0

快樂編碼我的朋友 – Ghimire