我对这段代码有疑问:
byte[] dataToPost = System.IO.File.ReadAllBytes(filePath);
当我在 Application.persistentDataPath 中使用图像的文件路径时,一切正常。
但是我无法将这样的 blob URL 用于 filePath: “blob:http://localhost:8000/3dcf66b0-5052-4b2d-bc00-070255483b48”
它给了我以下错误:
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
在错误消息中,它似乎将 blob URL 搞乱了: “/blob:http://localhost:8000/3dcf66b0-5052-4b2d-bc00-070255483b48”
我现在使用 sumpfkraut 提供的以下协程,而不是使用 System.IO.File.ReadAllBytes 读取字节数组:
//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);
}
}