0

I am using the following code to upload a file in my Dropbox and I keep getting the object reference not set to an instance of an object error.

DropboxClient client = new DropboxClient("Token");

private void lnklblRunBackUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    try
    {
        Task a = Upload(@"C:\Users\HARNESS\Desktop\assm.txt", @"/Apps/ABC");          
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        Cursor.Current = Cursors.Default;
    }
}

private async Task Upload(string localPath, string remotePath)  
{
    const int ChunkSize = 4096 * 1024;
    using (var fileStream = File.Open(localPath, FileMode.Open))
    {
        if (fileStream.Length <= ChunkSize)
        {
            await this.client.Files.UploadAsync(remotePath, body: fileStream);
        }
        else
        {
            await this.ChunkUpload(remotePath, fileStream, ChunkSize);
        }
    }
}

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 memStream = new MemoryStream(buffer, 0, byteRead))
        {
            if (idx == 0)
            {
                var result = await this.client.Files.UploadSessionStartAsync(false, memStream);
                sessionId = result.SessionId;
            }
            else
            {
                var cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));

                if (idx == numChunks - 1)
                {
                    FileMetadata fileMetadata = await this.client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(path), memStream);
                    Console.WriteLine(fileMetadata.PathDisplay);
                }
                else
                {
                    await this.client.Files.UploadSessionAppendV2Async(cursor, false, memStream);
                }
            }
        }
    }
}

I am getting an error on

await this.client.Files.UploadAsync(remotePath, body: fileStream);

I don't seem to understand what part of this code is sending null value. Please help me!

Adarsh Ravi
  • 893
  • 1
  • 16
  • 39

0 Answers0