使用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
       }