mercredi 22 juin 2016

Failed to upload image over custom server

Using web service, I was trying to upload image to web server. At present I want to upload profile picture to custom server but in this always, I was getting this error in Unity editor.

enter image description here

For this, I was using following code:

IEnumerator RegisterVoterProfile ()
 {
     WWW localFile = new WWW("file:///Users/gaminguruz/Desktop/profile_photo.png");
     yield return localFile;

     if (localFile.error == null)
         Debug.Log("Loaded file successfully");
     else
     {
         Debug.Log("Open file error: "+localFile.error);
         yield break; // stop the coroutine here
     }

     Dictionary<string,string> headerDisc = new Dictionary<string, string> ();
     headerDisc.Add ("Api-Key", "");
     headerDisc.Add("Content-Type","application/x-www-form-urlencoded");

     WWWForm form = new WWWForm ();
     form.AddField (GameConstants.REGISTRATION_USERNAME_ARG, userNameInput.text);
     form.AddField (GameConstants.REGISTRATION_PASSWORD_ARG, passwordInput.text);
     form.AddField (GameConstants.REGISTRATION_EMAIL_ARG, emailInput.text);
     form.AddBinaryData (GameConstants.REGISTRATION_PROFILE_PIC_ARG, localFile.bytes,"profile_photo.png","image/png");
     form.AddField (GameConstants.REGISTRATION_USERTYPE_ARG, GameConstants.VOTER_USER_TYPE);
     byte[] rawData = form.data;

     WWW www = new WWW (GameConstants.REGISTRATION_BASE_URL, rawData, headerDisc);
     yield return www;

     if (www.error == null) {
         Debug.Log ("Data: " + www.text);
         JSONObject jsonObj = new JSONObject (www.text);
         JSONObject messageObj = jsonObj [TAG_MESSAGE];

         string successValueStr = jsonObj [TAG_SUCCESS].ToString ();
         if (successValueStr.Equals (VALUE_TRUE)) {
             // login success
         } else {
             // login fail
         }
     } else {
         Debug.Log ("Error: " + www.error);
     }
 }

Please suggest me at which place I have done mistake.




Aucun commentaire:

Enregistrer un commentaire