2017-04-02 9 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を確認してください試すことができます-with-other-parameters –

+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

関連する問題