使用rest api将文件上传到onedrive时显示进度条 [英] show progressbar when upload file to onedrive using rest api

查看:458
本文介绍了使用rest api将文件上传到onedrive时显示进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用onedrive rest api将文件上传到onedrive。它使用client.asyncupload方法将文件上传到onedrive。它工作正常,但无法显示要上传的文件的百分比或进度。有人可以有任何参考吗?



以下是我上传文件到onedrive的代码。



Hi,
i am using onedrive rest api to upload files to onedrive. it uses client.asyncupload method to upload files to the onedrive. it works fine but not able to show percentage or progress about the file to be upload. can someone have any ref for this?

below is my code to upload file to the onedrive.

private void cmdBrowseAndUpload_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            using (var dlg = new OpenFileDialog())
            {
                dlg.Filter = "All Files|*.*";
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    fileName = dlg.FileName;
                }
            }
            string url = string.Format(@"https://apis.live.net/v5.0/me/skydrive/my_documents/files/{0}?access_token={1}",
                Path.GetFileName(fileName), access_token);
            using (var client = new WebClient())
            {
                client.UploadDataAsync(new Uri(url), "PUT", ImageToByteArray(fileName));
            }
        }

推荐答案

你好



使用Web客户端UploadProgressChanged事件



Hello

Use the web client UploadProgressChanged event

private void cmdBrowseAndUpload_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            using (var dlg = new OpenFileDialog())
            {
                dlg.Filter = "All Files|*.*";
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    fileName = dlg.FileName;
                }
            }
            string url = string.Format(@"https://apis.live.net/v5.0/me/skydrive/my_documents/files/{0}?access_token={1}",
                Path.GetFileName(fileName), access_token);
            using (var client = new WebClient())
            {
client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
                client.UploadDataAsync(new Uri(url), "PUT", ImageToByteArray(fileName));
            }
        }










void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
       {
           //To do you code here.
           //Progress changed code here
       }


这篇关于使用rest api将文件上传到onedrive时显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆