Junaid Pathan
Junaid Pathan

Reputation: 4336

How to Upload multiple files to a server using UnityWebRequest.Post();

I am trying to upload multiple files using UnityWebRequest.Post(), here's my code.

 public void UploadFiles()
 {
     string[] path = new string[3];
     path[0] = "D:/File1.txt";
     path[1] = "D:/File2.txt";
     path[2] = "D:/File3.txt";

     UnityWebRequest[] files = new UnityWebRequest[3];
     WWWForm form = new WWWForm();

     for (int i = 0; i < files.Length; i++)
     {
         files[i] = UnityWebRequest.Get(path[i]);
         form.AddBinaryData("files[]", files[i].downloadHandler.data, Path.GetFileName(path[i]));
     }

     UnityWebRequest req = UnityWebRequest.Post("http://localhost/File%20Upload/Uploader.php", form);
     yield return req.SendWebRequest();

     if (req.isHttpError || req.isNetworkError)
         Debug.Log(req.error);
     else
         Debug.Log("Uploaded " + files.Length + " files Successfully");
 }

The files are however created at the destination with size 0 bytes.

Here is my Uploader.php Code

 <$php
   $total = count($_FILES['files']['name']);
   $uploadError = false;
   for ( $i = 0; $i < $total; $i++)
   {
     $tmpFilePath = $_FILES['files']['tmp_name'][$i];

     if ($tmpFilePath != "")
     {
         $newFilePath = "Uploads/".$_FILES['files']['name'][$i];
         if (!move_uploaded_file($tmpFilePath, $newFilePath))
             $uploadError = true;
     }
   }
   if ($uploadError)
       echo "Upload Error";
   else
       echo "Uploaded Successfully";
 ?>

I used this HTML Sample for reference. While in browser HTML code works perfectly. There is problem in Unity.

 <form enctype="multipart/form-data" action="Uploader.php" method="POST">
     Choose a file to Upload:
     <input type="file" name="files[]" multiple="multiple" /><br>
     <input type="submit" value="Upload File" />
 </form>

Upvotes: 1

Views: 5327

Answers (1)

Junaid Pathan
Junaid Pathan

Reputation: 4336

In for loop, in the C# code, after requesting the file, we must yield while the file is fetched. so using yield return files[i].SendWebRequest(); after requesting the file will solve the problem. Here is the modified code:

IEnumerator UploadMultipleFiles()
{
    string[] path = new string[3];
    path[0] = "D:/File1.txt";
    path[1] = "D:/File2.txt";
    path[2] = "D:/File3.txt";

    UnityWebRequest[] files = new UnityWebRequest[path.Length];
    WWWForm form = new WWWForm();

    for (int i = 0; i < files.Length; i++)
    {
        files[i] = UnityWebRequest.Get(path[i]);
        yield return files[i].SendWebRequest();
        form.AddBinaryData("files[]", files[i].downloadHandler.data, Path.GetFileName(path[i]));
    }

    UnityWebRequest req = UnityWebRequest.Post("http://localhost/File%20Upload/Uploader.php", form);
    yield return req.SendWebRequest();

    if (req.isHttpError || req.isNetworkError)
        Debug.Log(req.error);
    else
        Debug.Log("Uploaded " + files.Length + " files Successfully");
}

Rest of the code is fine. No changes in PHP code. HTML code is only for reference.

Upvotes: 1

Related Questions