I am creating a file upload application for Android and iOS using Xamarin PCL, and I managed to implement file download and some kind of progress bar, but it does not work correctly.
I saw several responses to the stack overflow to display the download, but I want to notify my users of the download progress and have not found any solution.
Here is my code:
public static async Task<string> PostFileAsync (Stream filestream, string filename, int filesize) {
var progress = new System.Net.Http.Handlers.ProgressMessageHandler ();
progress.HttpSendProgress += (object sender, System.Net.Http.Handlers.HttpProgressEventArgs e) => {
int progressPercentage = (int)(e.BytesTransferred*100/filesize);
UploadProgressMade(sender, new System.Net.Http.Handlers.HttpProgressEventArgs(progressPercentage, null, e.BytesTransferred, null));
};
using (var client = HttpClientFactory.Create(progress)) {
using (var content = new MultipartFormDataContent ("------" + DateTime.Now.Ticks.ToString ("x"))) {
content.Add (new StreamContent (filestream), "Filedata", filename);
using (var message = await client.PostAsync ("http://MyUrl.example", content)) {
var result = await message.Content.ReadAsStringAsync ();
System.Diagnostics.Debug.WriteLine ("Upload done");
return result;
}
}
}
}
Some progress is displayed, but when the progress reaches 100%, the file is not yet loaded. The message “Download done” is also printed some time after I received the last progress message.
, , , , , , 100%, , ?
: : https://forums.xamarin.com/discussion/56716/plans-to-add-webclient-to-pcl, .