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

I have a problem with this code:

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:

DirectoryNotFoundException: Could not find a part of the path "/blob:http:/localhost:8000/3dcf66b0-5052-4b2d-bc00-070255483b48".
  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”

I think you can not use ReadAllBytes in WebGL.

//Load the byte[] from blob or from url.
    private IEnumerator LoadFile(string url)
    {
        using UnityWebRequest uwr = UnityWebRequest.Get(url);
        yield return uwr.SendWebRequest();
        if (uwr.error != null) Debug.Log(uwr.error);
        else
        {
            byte[] result = new byte[uwr.downloadHandler.data.Length];
            System.Array.Copy(uwr.downloadHandler.data, 0, result, 0, uwr.downloadHandler.data.Length);
            Debug.Log("Loaded file size: " + uwr.downloadHandler.data.Length + " bytes");
            //Do something with the byte array now...
            ByteResultExamples(result);
        }
    }

Thanks for your reply sumpfkraut. To select the image file I use your code which you have provided here:

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:

UnityWebRequest uwr;

        IEnumerator PostRequest(string url, string filePath)
        {
            byte[] dataToPost = System.IO.File.ReadAllBytes(filePath);
            UploadHandlerRaw uhr = new UploadHandlerRaw(dataToPost);

            uwr = new UnityWebRequest(url, "POST", new DownloadHandlerBuffer(), uhr);
            uwr.SetRequestHeader("Accept", "application/json");
            uwr.SetRequestHeader("Authorization", "Bearer " + apiToken);
            uwr.SetRequestHeader("Content-Type", "*/*");

            yield return uwr.SendWebRequest();

            if (uwr.isNetworkError)
            {
                Debug.Log("Error While Sending: " + uwr.error);
            }
            else
            {
                Debug.Log("Received: " + uwr.downloadHandler.text);
                NFTStorageCustomUploadResponse parsedResponse = JsonUtility.FromJson<NFTStorageCustomUploadResponse>(uwr.downloadHandler.text);
                GetComponent<MainScript>().NFTStorageUploadComplete(parsedResponse.value.cid);            
            }
        }

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!!!