Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UnityWebRequest (Legacy function) working but IMultipartFormSection not

Discussion in 'Scripting' started by rattlesnake, Apr 1, 2017.

  1. rattlesnake

    rattlesnake

    Joined:
    Jul 18, 2013
    Posts:
    138
    Good morning,

    I'm trying do make a POST with an image. A friend configured a server to then send back the photo via mail.

    Doing the following with the lecacy converter it works perfectly:
    Code (CSharp):
    1.  
    2. IEnumerator UploadLegacy()
    3. {
    4.     byte[] bytes = File.ReadAllBytes(attachmentPath);
    5.  
    6.     WWWForm form = new WWWForm();
    7.     form.AddField("myField", "myData");
    8.     //image
    9.     form.AddBinaryData("file1", bytes, "pictureName.jpg", "image/jpeg");
    10.    
    11.     string URL = "myURL";
    12.  
    13.     UnityWebRequest www = UnityWebRequest.Post (URL, form);
    14.  
    15.     yield return www.Send();
    16.  
    17.     if (www.isError)
    18.     {
    19.         Debug.Log(www.error);
    20.     }
    21.     else
    22.     {
    23.         Debug.Log("Form upload complete!");
    24.     }
    25. }
    26.  
    But using IMultipartFormSection, it's half working. I have the log "Form upload complete!" but the server is never sending the photo back.
    Code (CSharp):
    1.  
    2. IEnumerator Upload()
    3. {
    4.     byte[] bytes = File.ReadAllBytes(attachmentPath);
    5.    
    6.     List<IMultipartFormSection> form = new List<IMultipartFormSection>();
    7.     form.Add(new MultipartFormDataSection("myField","myData"));
    8.     //image
    9.     form.Add( new MultipartFormFileSection("file1", bytes, "pictureName.jpg","image/jpeg") );
    10.    
    11.     string URL = "myURL";
    12.  
    13.     UnityWebRequest www = UnityWebRequest.Post (URL, form);
    14.  
    15.     yield return www.Send();
    16.  
    17.     if (www.isError)
    18.     {
    19.         Debug.Log(www.error);
    20.     }
    21.     else
    22.     {
    23.         Debug.Log("Form upload complete!");
    24.     }
    25. }
    26.  
    Is it an issue of formating or something ?
     
  2. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
  3. GeekyMonkey

    GeekyMonkey

    Joined:
    Feb 9, 2013
    Posts:
    13
    I think what you're seeing here is that the Send() function is returning before the file is completely uploaded. It's uploading the file in chunks of bytes. When each chunk is transferred it yeilds, this gives you a chance to update a UI element to show the progress of the upload.

    try putting this after your Send() call

    while (www.isDone == false)
    {
    // You could update a UI element with the progress with something like this:
    // MyProgressSlider.value = www.downloadProgress;
    yield return null;
    }
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    I believe this bug has been fixed. You may simply need to upgrade to newer version of Unity.