Hi everyone,
I'm making a Xamarin.Forms Android application with MvvmLight. I've got a ViewModel which consume a .NET WebAPI to download a File.
I'd like to display a download progress bar for the user which i did with the following code (From XF Documentation):
var method = SharedConstants.API.GET_MISSION;
long receivedBytes = 0;
long totalBytes = 0;
using(var client = APIHttpClient.GetClient())
{
using(var stream = await client.GetStreamAsync(method))
{
byte[] buffer = new byte[4096];
totalBytes = stream.Length;
for(;;) {
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if(bytesRead == 0) {
await Task.Yield();
break;
}
receivedBytes += bytesRead;
int received = unchecked((int)receivedBytes);
int total = unchecked((int)totalBytes);
double percentage = ((float)received) / total;
ProgressValue = percentage;
}
// -> Want to save the file localy -> need the whole byte[]
}
}
The problem is i want to save the file locally on the mobile phone and i don't know how to have the whole byte array without making another call with GetByteAsync(). Well it seems trivial but i don't see how i can do that. One way is to call client.GetByteAsync() but i won't be able to show a progress to the user. Maybe i'm missing something obvious !
Thanks,
Best regards,
Armand