Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

RAJIB N.'s avatar
RAJIB N.
New member | Level 1
10 years ago

How to get Progress Status while uploading/ downloading a file ( in .Net ) ?

I am using DropboxClient.Files.UploadAsync method for uploading file and 

DropboxClient.Files.DownloadAsync for downloading.

Uploading and Downloading works perfectly but can't get the progress status.

A example in C# will be very helpful.

 

5 Replies

Replies have been turned off for this discussion
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    The .NET SDK doesn't offer native progress listeners, but I'll be sure to pass this along as a feature request. You can however track download progress using GetContentAsStreamAsync. E.g., you can do something like:

    var response = await client.Files.DownloadAsync(path);
    ulong fileSize = response.Response.Size;
    const int bufferSize = 1024 * 1024;

    var buffer = new byte[bufferSize];

    using (var stream = await response.GetContentAsStreamAsync())
    {
    using (var file = new FileStream("Test", FileMode.OpenOrCreate))
    {
    var length = stream.Read(buffer, 0, bufferSize);

    while (length > 0)
    {
    file.Write(buffer, 0, length);
    var percentage = 100 * (ulong)file.Length / fileSize;
    // Update progress bar with the percentage.
    // progressBar.Value = (int)percentage
    Console.WriteLine(percentage);

    length = stream.Read(buffer, 0, bufferSize);
    }
    }
    }

    You can do something similar for uploads using upload sessions (i.e., update your progress between chunks).

  • RAJIB N.'s avatar
    RAJIB N.
    New member | Level 1
    10 years ago

    Thanks for the reply.

    Anyway, could you send me the C# code for upload as I can not get any complete solutions in the net. Also I am not sure how can I upload files > 150 MB to Dropbox. 

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    Here's a sample of uploading using upload sessions:

    private async Task ChunkUpload(String path, FileStream stream, int chunkSize)
    {
    int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
    byte[] buffer = new byte[chunkSize];
    string sessionId = null;
    for (var idx = 0; idx < numChunks; idx++)
    {
    var byteRead = stream.Read(buffer, 0, chunkSize);

    using (var memSream = new MemoryStream(buffer, 0, byteRead))
    {
    if (idx == 0)
    {
    var result = await this.client.Files.UploadSessionStartAsync(memSream);
    sessionId = result.SessionId;
    }
    else
    {
    var cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));

    if (idx == numChunks - 1)
    {
    await this.client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(path), memSream);
    }
    else
    {
    await this.client.Files.UploadSessionAppendAsync(cursor, memSream);
    }
    }
    }
    }
    }
  • RAJIB N.'s avatar
    RAJIB N.
    New member | Level 1
    10 years ago

    Hello,

    I have tried with downloading and uploading functions supplied by you, it gives the progress data alright, but I can not set that data to progress bar. I have worked with couple of hours but can not place that process status in the progress bar or any other way. I have attached codes of Upload 

     

    private async Task ChunkUpload()
    {

    string path = UploadFolder + "/" + UploadFile;
    FileStream stream = UploadFileStream;
    int chunkSize = 128 * 1024;

    DropboxClient dbx = new DropboxClient(SystemVariables.WebAcccessToken);
    int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
    byte[] buffer = new byte[chunkSize];
    string sessionId = null;



    for (var idx = 0; idx < numChunks; idx++)
    {


    var byteRead = stream.Read(buffer, 0, chunkSize);

    using (var memSream = new MemoryStream(buffer, 0, byteRead))
    {
    if (idx == 0)
    {
    var result = await dbx.Files.UploadSessionStartAsync(false, memSream);
    sessionId = result.SessionId;
    }
    else
    {
    var cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
    var percentage = 100 * ((chunkSize * idx) / (double)stream.Length);
    pValue = (int)Math.Ceiling((decimal)percentage);



    // Approach 1

    var progressIndicator = new Progress<int>(ReportProgress);
    int uploads = await UploadProgress(progressIndicator);


    // Approach 2

    /*
    Thread t = new Thread(new ThreadStart(this.DoProcess));

    t.Start();

    while (t.IsAlive) // while thread is running
    {

    Application.DoEvents(); // Allow UI to refresh if needed

    Thread.Sleep(10000); // sleep a while to avoid processor load
    t.Abort();
    }*/


    if (idx == numChunks - 1)
    {
    await dbx.Files.UploadSessionFinishAsync(cursor, new CommitInfo(path), memSream);
    }
    else
    {
    await dbx.Files.UploadSessionAppendV2Async(cursor,false, memSream);

    }
    }
    }
    }
    }





    public delegate void UpdateProgressBarDelegate(int progress);
    public UpdateProgressBarDelegate UpdateProgressBar;

    private void UpdateProgressBarMethod(int progress)
    {
    IAsyncResult result = this.progressBar1.BeginInvoke(
    (MethodInvoker)delegate()
    {
    progressBar1.Value = progress;
    Text = progress.ToString();
    //txtLogDetails.Text = message;

    });
    }

    private void DoProcess()
    {
    UpdateProgressBarMethod(pValue);
    }






    void ReportProgress(int value)
    {
    progressBar1.Value = value;
    }

    async Task<int> UploadProgress(IProgress<int> progress)
    {
    int processCount = await Task.Run<int>(() =>
    {
    if (progress != null)
    {
    progress.Report(pValue);
    }

    return pValue;
    });
    return processCount;
    }



    You can see the 2 approaches that I have tried to update progress bar value,
    but can not see the update in progress bar.

    It will be great if you provide the detail code for display updates in progress bar from the scratch.










  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    It sounds like the Dropbox part of this is working, and you're having trouble with the UI side of this. That's outside the scope of Dropbox API support, so I'm afraid I can't be of any help with that.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!