Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Open a File Stream in WebGL

Discussion in 'WebGL' started by Reddevildragg, Mar 18, 2019.

  1. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    43
    Hi

    Is there any way to open a file stream from a file stored in streaming assets? Currently thought i may have a way of doing it but whenever i then try and look into the database for information it crashes and throws memory access errors.

    I have a database in the streaming assets that is able to run online but i need to be able to access this information. Other thinking is can i download and cache this locally and then file stream from the local file in Temp or somewhere else (I have seen a few ways to upload an image from your desktop and wondering if this would work with a file stream)

    Thanks
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    StreamingAssets files must downloaded to be able to access them.
     
  3. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    43
    Hi
    So i have got a unity web request accessing the file and giving me back the byte[] data then using this to create a memory stream which i was hoping would allow me to access the data. however although the memory stream is created and contains the correct number of bytes, I am unable to then access the stream afterwards. Any idea what could be causing this?
     
  4. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Could you post your code?
     
  5. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    43
    Code (CSharp):
    1.     MemoryStream theMemStream;
    2.  
    3.     // Start is called before the first frame update
    4.     IEnumerator Start()
    5.     {
    6.        yield return StartCoroutine(Stream());
    7.      
    8.         Debug.Log(theMemStream.Length);
    9.  
    10.         LiteDatabase liteDatabase = new LiteDatabase(theMemStream);
    11.  
    12.         IEnumerable<string> x = liteDatabase.GetCollectionNames();
    13.         foreach (string variable in x)
    14.         {
    15.             Debug.Log(variable);
    16.         }
    17.  
    18.  
    19.         Debug.Log("Hello");
    20.         //Debug.Log(liteDatabase.GetCollection<MyData>().Count());
    21.         List<MyData> data = liteDatabase.GetAllCollectionData<MyData>();
    22.        Debug.Log(data.Count);
    23.        foreach (MyData myData in data)
    24.        {
    25.            Debug.Log(myData.Message);
    26.            message = myData.Message;
    27.        }
    28.        Debug.Log("Do we even get here");
    29.     }
    30.  
    31.     IEnumerator Stream()
    32.     {
    33.  
    34.         string url = Path.Combine(Application.streamingAssetsPath, "Blank.db");
    35.         Debug.Log(url);
    36.         //UnityWebRequest request = new UnityWebRequest();
    37.         byte[] dbData;
    38.      
    39.         //Check if we should use UnityWebRequest or File.ReadAllBytes
    40.         if (url.Contains("://") || url.Contains(":///"))
    41.         {
    42.             UnityWebRequest www = UnityWebRequest.Get(url);
    43.             yield return www.SendWebRequest();
    44.             dbData = www.downloadHandler.data;
    45.         }
    46.         else
    47.         {
    48.             dbData = File.ReadAllBytes(url);
    49.         }
    50.  
    51.        // yield return request.SendWebRequest();
    52.      
    53.         Debug.Log(dbData.Length);
    54.      
    55.       //  Debug.Log("Downloading File");
    56.       //  DownloadFile(imgData, imgData.Length, "blank.db");
    57.      
    58.         theMemStream = new MemoryStream();
    59.  
    60.         theMemStream.Write(dbData, 0, dbData.Length);
    61.      
    62.         Debug.Log($"Memory stream is {theMemStream.Length} bytes");
    63.     }
    This is what i have so far. Error being generated after the build it as followed
    "exception thrown: RuntimeError: index out of bounds,@blob:null/9fb8a57f-4ab1-44f8-8d21-4764a76e9f76 line 275 > WebAssembly.instantiate:wasm-function[14213]:0x5940aa"

    The get collections names outputs fine, but then the getting data within them causing this error
     
  6. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    It sounds like it crashes when you iterate on the list returned by GetAllCollectionData()

    I suggest you test it on a different il2cpp-platform (Mobile or Standalone, just make sure scripting backend is il2cpp). Make sure to host the file with a simple http server, then download it using UnityWebRequest like you would on WebGL.
     
  7. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    43
    Thanks ill give that a go and see what results i get out of that
     
unityunity