Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ Add byte[] download methods #64

Next Next commit
Add DownloadFileBytes method
  • Loading branch information
SimonDarksideJ committed Jan 6, 2024
commit 81744f2ba5e655ac8e18850626920c41d26501a5
29 changes: 29 additions & 0 deletions Utilities.Rest/Packages/com.utilities.rest/Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,35 @@ public static bool TryGetFileNameFromUrl(string url, out string fileName)
return filePath;
}

public static async Task<byte[]> DownloadFileBytesAsync(
string url,
string fileName = null,
RestParameters parameters = null,
bool debug = false,
CancellationToken cancellationToken = default)
{
await Awaiters.UnityMainThread;
byte[] bytes = null;

var filePath = await DownloadFileAsync(url, fileName, parameters, false, cancellationToken);
StephenHodgson marked this conversation as resolved.
Show resolved Hide resolved
var absolutefilePath = filePath.Replace("file:https://", "");
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
if (File.Exists(absolutefilePath))
SimonDarksideJ marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
bytes = File.ReadAllBytes(absolutefilePath);
}
catch (Exception ex)
{
Debug.LogError(ex);
throw;
StephenHodgson marked this conversation as resolved.
Show resolved Hide resolved
}
}

return bytes;
}


#endregion Get Multimedia Content

/// <summary>
Expand Down