Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
Sayak B.
10 years agoExplorer | Level 3
Dropbox API in C# how to reuse the session id of bulk file upload in case file could not be uploaded by any issue.
In case of uploading big file of more than 100 mb, if by some case the internet connection get dropped then how it will be possible to upload the same file from the chunk it has been failed to upload...
Sayak B.
10 years agoExplorer | Level 3
I am using Dropbox .NET library.
The problem is. I am actually trying to upload my sql server database backup everyday by zipping it to DropBox. The backup files after zip is average 50 mb in size. But the reality is while upload the internet get disconnected several time. So I need to upload the same thing fresh again and again the internet connection issue happens, The database server is in a remote location where internet connection is not so good. The result is that the file could not ever been uploaded because of the said issue, What I am trying to do is to use the upload session id which I am getting after uploading the first chunk. I am keeping it in a xml file and checking if the connection is failed and the file could not been uploaded then restart uploading the same file from the last successful uploaded chunk by using the session id. Below is my code snippet.
async void ConnectDropBox()
{
await LoginDropBox();
var progress = new Progress<ProgressReport>();
progress.ProgressChanged += (o, e) =>
{
Dispatcher.BeginInvoke((Action)(() =>
{
pgbarUpload.Maximum = e.TotalSteps;
pgbarUpload.Value = e.CurrentStep;
lblStatus.Text = "Uploading slice " + e.CurrentStep.ToString() + " of " + e.TotalSteps;
File.WriteAllText(AppVariables.xmlFilePath, AppVariables.CurrentUpload.ToXml<xmlUpload>());
if (e.CurrentStep == e.TotalSteps)
{
lblStatus.Text = string.Format("File \"{0}\" uploaded successfully with {1} slices", Path.GetFileName(AppVariables.ZipFilePath), e.TotalSteps);
btnUpload.IsEnabled = true;
btnRefresh.IsEnabled = true;
}
}));
};
try
{
await Upload(progress);
}
//catch (InvalidOperationException) { }
catch (Exception ex)
{
JHMessageBox.Show(ex.Message, "Exception Occured !", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async Task Upload(IProgress<ProgressReport> progress)
{
try
{
const int chunkSize = 128 * 1024;
bool IsPrevUploadFailed = false;
using (var stream = new FileStream(AppVariables.ZipFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
var progressReport = new ProgressReport() { TotalSteps = numChunks };
byte[] buffer = new byte[chunkSize];
string sessionId = null;
await Task.Run(async () =>
{
for (var idx = 0; idx < numChunks; idx++)
{
if (!AppVariables.LastUpload.IsNull() && !IsPrevUploadFailed)
{
if (AppVariables.CurrentUpload.Date == AppVariables.LastUpload.Date &&
AppVariables.CurrentUpload.FileChecksum == AppVariables.LastUpload.FileChecksum &&
AppVariables.CurrentUpload.FileName == AppVariables.LastUpload.FileName)
{
sessionId = AppVariables.LastUpload.SessionId;
IsPrevUploadFailed = true;
}
}
var byteRead = stream.Read(buffer, 0, chunkSize);
using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if ((idx == 0 && !IsPrevUploadFailed) || (IsPrevUploadFailed && !AppVariables.LastUpload.IsNull() && AppVariables.LastUpload.Slice == 0))
{
var result = await AppVariables.DrpBoxClient.Files.UploadSessionStartAsync(body: memStream);
sessionId = result.SessionId;
}
else
{
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
if (!IsPrevUploadFailed || (IsPrevUploadFailed && !AppVariables.LastUpload.IsNull()))
{
if (idx == numChunks - 1)
{
await AppVariables.DrpBoxClient.Files.UploadSessionFinishAsync(cursor, new CommitInfo(Config.DropBoxDirectory + System.IO.Path.GetFileName(AppVariables.ZipFilePath)), memStream);
}
else
{
await AppVariables.DrpBoxClient.Files.UploadSessionAppendV2Async(cursor, body: memStream);
}
}
}
}
AppVariables.CurrentUpload.SessionId = sessionId;
AppVariables.CurrentUpload.Slice = idx;
AppVariables.CurrentUpload.FileName = AppVariables.ZipFileName;
progressReport.CurrentStep = idx + 1;
progressReport.PercentComplete = (idx + 1) * 100 / progressReport.TotalSteps;
progress.Report(progressReport);
}
});
stream.Close();
stream.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
public class ProgressReport
{
public int PercentComplete { get; set; }
public int CurrentStep { get; set; }
public int TotalSteps { get; set; }
}
public class xmlUpload
{
public DateTime Date { get; set; }
public string FileChecksum { get; set; }
public string FileName { get; set; }
public int Slice { get; set; }
public string SessionId { get; set; }
}
About 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!