Click here to Skip to main content
15,886,851 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi,
I want to upload some text files from my C# .Net web application to dropbox account. I have created dropbox App but don't getting clear idea how accomplish that.
Please help me to provide steps to upload files from C# .Net web application to dropbox account.

Thanks!
Naveen

What I have tried:

I have installed Dropbox.Api.dll and DropNet.dll from nuget Package manager. Buut not sure how to proceed further.
Posted
Updated 4-Jun-18 7:33am

Grab the DropBox lib from here: .NET - Developers - Dropbox[^]

Get your API key from here: Login - Dropbox[^]

And here is how: .NET - Developers - Dropbox[^]
 
Share this answer
 
The solution to this is simple and I would highlight the procedure thus:

1.)Create an app from Dropbox App Console. Choose App Folder (this is a preferable option)  or Full Dropbox for Access Type; enter a name for your app (e.g. myapp). Click "Create App" button. 
Another page is displayed, click on "Generate" button under Generated Access Token.
For the rest of this write-up, the generated code will be called "token".

2.)In your Program.cs (assuming you're using Console App), ensure the following namespaces are referenced

C#
using System;
using System.Threading.Tasks;
using Dropbox.Api;
using System.IO;

Then, modify your your code to

C#
class Program
{
  static void Main(string[] args)
  {
     var task = Task.Run(Upload);
     task.Wait();
  }

  static async Task Upload()
  {
    var dbx = new DropboxClient("token");
    var filePath = @"c:\myFile.txt"; //for example

    if(File.Exists(filePath))
    {
      var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    
      byte[] content;
      using(var reader = new BinaryReader(fileStream))
      {
         content = reader.ReadBytes((int)fileStream.Length);
      }

      using(var mem = new MemoryStream(content))
      {
         await dbx.Files.UploadAsync("/myFile.txt", WriteMode.Overwrite.Instance, body: 
               mem);
      }
    }
  }
}


3.) Add Newtonsoft.Json package using NuGet Package Manager and you are done.

There is a better approach to uploading bigger-sized files. But I hope this suites your scenario.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900