Created
November 12, 2012 10:42
-
-
Save buildmaster/4058597 to your computer and use it in GitHub Desktop.
async with progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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