android asynchttpclient 进度条,如何使用C#HttpClient PostAsync显示上传进度

我正在使用Xamarin PCL为Android和iOS创建文件上传应用程序,我已设法实现文件上传和某种进度条,但它无法正常工作.

我看到堆栈溢出的一些答案用于显示下载进度,但我想通知我的用户有关上传进度的信息,但没有找到任何解决方案.

这是我的代码:

public static async Task PostFileAsync (Stream filestream, string filename, int filesize) {

var progress = new System.Net.Http.Handlers.ProgressMessageHandler ();

//Progress tracking

progress.HttpSendProgress += (object sender, System.Net.Http.Handlers.HttpProgressEventArgs e) => {

int progressPercentage = (int)(e.BytesTransferred*100/filesize);

//Raise an event that is used to update the UI

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;

}

}

}

}

显示某种进度,但当进度达到100%时,文件尚未上载.在收到最后一条进度消息后的某个时间也会打印消息“上传已完成”.

也许进展是显示从设备发出的字节而不是已经上传的字节,所以当它说是100%时,所有字节都只是发出但服务器尚未收到?

解决方法:

尝试这样的事情:

我遇到了同样的问题.我通过实现自定义HttpContent来修复它.我使用此对象来跟踪上传进度的百分比,您可以添加事件并收听它.您应该自定义SerializeToStreamAsync方法.

internal class ProgressableStreamContent : HttpContent

{

private const int defaultBufferSize = 4096;

private Stream content;

private int bufferSize;

private bool contentConsumed;

private Download downloader;

public ProgressableStreamContent(Stream content, Download downloader) : this(content, defaultBufferSize, downloader) {}

public ProgressableStreamContent(Stream content, int bufferSize, Download downloader)

{

if(content == null)

{

throw new ArgumentNullException("content");

}

if(bufferSize <= 0)

{

throw new ArgumentOutOfRangeException("bufferSize");

}

this.content = content;

this.bufferSize = bufferSize;

this.downloader = downloader;

}

protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)

{

Contract.Assert(stream != null);

PrepareContent();

return Task.Run(() =>

{

var buffer = new Byte[this.bufferSize];

var size = content.Length;

var uploaded = 0;

downloader.ChangeState(DownloadState.PendingUpload);

using(content) while(true)

{

var length = content.Read(buffer, 0, buffer.Length);

if(length <= 0) break;

downloader.Uploaded = uploaded += length;

stream.Write(buffer, 0, length);

downloader.ChangeState(DownloadState.Uploading);

}

downloader.ChangeState(DownloadState.PendingResponse);

});

}

protected override bool TryComputeLength(out long length)

{

length = content.Length;

return true;

}

protected override void Dispose(bool disposing)

{

if(disposing)

{

content.Dispose();

}

base.Dispose(disposing);

}

private void PrepareContent()

{

if(contentConsumed)

{

// If the content needs to be written to a target stream a 2nd time, then the stream must support

// seeking (e.g. a FileStream), otherwise the stream can't be copied a second time to a target

// stream (e.g. a NetworkStream).

if(content.CanSeek)

{

content.Position = 0;

}

else

{

throw new InvalidOperationException("SR.net_http_content_stream_already_read");

}

}

contentConsumed = true;

}

}

参考:

标签:c,file-upload,xamarin,httpclient,progress

来源: https://codeday.me/bug/20190516/1116050.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值