cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

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

RAJIB N.
New member | Level 1

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 5

Greg-DB
Dropbox Staff

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.
New member | Level 1

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
Dropbox Staff

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.
New member | Level 1

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
Dropbox Staff

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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    RAJIB N. New member | Level 1
What do Dropbox user levels mean?