Skip to content

Instantly share code, notes, and snippets.

@buildmaster
Created November 12, 2012 10:42
Show Gist options
  • Save buildmaster/4058597 to your computer and use it in GitHub Desktop.
Save buildmaster/4058597 to your computer and use it in GitHub Desktop.
async with progress
var cl = new FileInfo(photo.ImageLocation).Length;
request.Files.Add(new FileParameter
{
ContentLength = cl,
Name = "photo[photo]",
FileName = Path.GetFileName(photo.ImageLocation),
Writer = s =>
{
using (var file = File.OpenRead(photo.ImageLocation))
{
byte[] buffer = new byte[16384]; //16k buffer
int bytesRead = 0;
int read = 0;
while ((read = file.Read(buffer, 0, buffer.Length)) > 0)
{
s.Write(buffer, 0, read);
s.Flush();
bytesRead += read;
System.Diagnostics.Debug.WriteLine("uploading: " + (int)((double)bytesRead / file.Length * 100) + "%");
_messengerHub.Publish(new PhotoUploadProgressed { Sender = null, Photo = photo, Uploaded = bytesRead });
}
file.Close();
}
}
});
client.ExecuteAsync(request, (response) =>
{
_messengerHub.PublishAsync(new PhotoUploadFinishedMessage
{
Sender = null,
Photo = photo,
Response = response.Content,
Cancelled = false,
Error = response.ErrorException
});
Console.WriteLine("finished");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment