Search Unity

Question System.IO.File.ReadAllBytes not working with blob URL

Discussion in 'Web' started by aronsommer, Jan 4, 2024.

  1. aronsommer

    aronsommer

    Joined:
    Dec 5, 2010
    Posts:
    202
    I have a problem with this code:
    Code (CSharp):
    1. byte[] dataToPost = System.IO.File.ReadAllBytes(filePath);
    When I use the filePath of an image in Application.persistentDataPath everything works fine.

    But I am not able to use a blob URL like this for filePath:
    "blob:http://localhost:8000/3dcf66b0-5052-4b2d-bc00-070255483b48"

    It gives me following error:
    Code (CSharp):
    1. DirectoryNotFoundException: Could not find a part of the path "/blob:http:/localhost:8000/3dcf66b0-5052-4b2d-bc00-070255483b48".
    2.   at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00000] in <00000000000000000000000000000000>:0
    In the error message it seems it messes up the blob URL to:
    "/blob:http:/localhost:8000/3dcf66b0-5052-4b2d-bc00-070255483b48"
     
  2. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    I think you can not use ReadAllBytes in WebGL.

    Code (CSharp):
    1.  
    2. //Load the byte[] from blob or from url.
    3.     private IEnumerator LoadFile(string url)
    4.     {
    5.         using UnityWebRequest uwr = UnityWebRequest.Get(url);
    6.         yield return uwr.SendWebRequest();
    7.         if (uwr.error != null) Debug.Log(uwr.error);
    8.         else
    9.         {
    10.             byte[] result = new byte[uwr.downloadHandler.data.Length];
    11.             System.Array.Copy(uwr.downloadHandler.data, 0, result, 0, uwr.downloadHandler.data.Length);
    12.             Debug.Log("Loaded file size: " + uwr.downloadHandler.data.Length + " bytes");
    13.             //Do something with the byte array now...
    14.             ByteResultExamples(result);
    15.         }
    16.     }
     
  3. aronsommer

    aronsommer

    Joined:
    Dec 5, 2010
    Posts:
    202
    Thanks for your reply sumpfkraut. To select the image file I use your code which you have provided here:
    https://forum.unity.com/threads/how...from-their-harddrive-into-a-webgl-app.380985/

    Displaying the image from the blob address as texture ingame works fine. I have already a method for this.

    The problem is the uploading to the NFT.Storage. It works fine when I use the filePath of an image in Application.persistentDataPath. ReadAllBytes works just fine in WebGL.

    But it just messes up the file path when I use the blob URL.

    My workaround is to save a new image from the generated texture to Application.persistentDataPath. But it would be so much better if I can upload the original image directly from the blob URL.

    I have to use this code in NFTStorageClient.cs to upload an image to NFT.Storage:

    Code (CSharp):
    1. UnityWebRequest uwr;
    2.  
    3.         IEnumerator PostRequest(string url, string filePath)
    4.         {
    5.             byte[] dataToPost = System.IO.File.ReadAllBytes(filePath);
    6.             UploadHandlerRaw uhr = new UploadHandlerRaw(dataToPost);
    7.  
    8.             uwr = new UnityWebRequest(url, "POST", new DownloadHandlerBuffer(), uhr);
    9.             uwr.SetRequestHeader("Accept", "application/json");
    10.             uwr.SetRequestHeader("Authorization", "Bearer " + apiToken);
    11.             uwr.SetRequestHeader("Content-Type", "*/*");
    12.  
    13.             yield return uwr.SendWebRequest();
    14.  
    15.             if (uwr.isNetworkError)
    16.             {
    17.                 Debug.Log("Error While Sending: " + uwr.error);
    18.             }
    19.             else
    20.             {
    21.                 Debug.Log("Received: " + uwr.downloadHandler.text);
    22.                 NFTStorageCustomUploadResponse parsedResponse = JsonUtility.FromJson<NFTStorageCustomUploadResponse>(uwr.downloadHandler.text);
    23.                 GetComponent<MainScript>().NFTStorageUploadComplete(parsedResponse.value.cid);            
    24.             }
    25.         }
     
    Last edited: Jan 4, 2024
  4. aronsommer

    aronsommer

    Joined:
    Dec 5, 2010
    Posts:
    202
    Thank you so much @sumpfkraut !!!

    I have now used your coroutine to load the byte[] from the blob url.
    Then I send the byte array to the PostRequest coroutine instead of the filePath.

    Works perfect! You made my day!!!