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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Uploading File to Server from Android

Discussion in 'Scripting' started by Pepn, Nov 24, 2021.

  1. Pepn

    Pepn

    Joined:
    Sep 27, 2017
    Posts:
    9
    Hello guys,

    I am uploading a .csv file to a webserver I host, running this on the computer works fine. However, when running this on Android the uploaded file is 0kb. Does anyone have any idea why this is?

    Thanks in advance,

    Code (CSharp):
    1.     IEnumerator Upload(string file)
    2.     {
    3.         uploadStatus = UploadStatus.started;
    4.  
    5.         string path = Application.persistentDataPath + "/results/Test.csv";
    6.  
    7.         FileStream filestream;
    8.         if (File.Exists(path)) filestream = File.OpenRead(path);
    9.         else
    10.         {
    11.             Debug.LogError("File not found");
    12.         }
    13.  
    14.         Debug.Log("Uploading File to Path: " + path);
    15.         WWWForm form = new WWWForm();
    16.         UnityWebRequest dataFile = UnityWebRequest.Get(path);
    17.         yield return dataFile.SendWebRequest();
    18.         form.AddBinaryData("dataFile", dataFile.downloadHandler.data, path, "text/csv");
    19.         UnityWebRequest req = UnityWebRequest.Post(uploadLink, form);
    20.         yield return req.SendWebRequest();
    21.  
    22.         uploadStatus = UploadStatus.completed;
    23.  
    24.         Debug.Log("SERVER: " + req.downloadHandler.text); // server response
    25.  
    26.         if (req.isHttpError || req.isNetworkError || !(req.downloadHandler.text.Contains("FILE OK")))
    27.             uploadStatus = UploadStatus.error;
    28.         else
    29.             uploadStatus = UploadStatus.successful;
    30.  
    31.         yield break;
    32.     }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,642
    The simplest way to do it is by using UploadHandlerFile, have a look at this:
    https://docs.unity3d.com/ScriptReference/Networking.UploadHandlerFile.html

    If you wonder, why your code doesn't work, the problem that you pass file path to UnityWebRequest.Get instead of URI. You probably are on a Windows computer, so it does recognize the file path, not so on Android (or other UNIX-like system). If you pass that path to System.Uri and pass that to UnityWebRequest, it should work.
    I don't understand, why you open file using File class and later read it using UnityWebRequest instead, when you can read it via stream you already have. But that's probably beside the point anyway.
     
    Pepn likes this.
  3. Pepn

    Pepn

    Joined:
    Sep 27, 2017
    Posts:
    9
    Thanks buddy, that worked.

    As for the File class, it was a quick hacked in test if the file did exist. It seemed to be that way, but perhaps I didn't see it in the sea of debug msgs.
     
  4. rathsocheat

    rathsocheat

    Joined:
    Sep 23, 2020
    Posts:
    2
    I got this error: Failed to read the request form. Multipart body length limit 16384 exceeded. On my server size already [DisableRequestSizeLimit] and I test upload the same file on swagger on my api it successfully.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780