Created
December 30, 2014 23:02
-
-
Save cmshawns/49537d4d53d8dd96bed4 to your computer and use it in GitHub Desktop.
Upload File to Dropbox with RestSharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void UploadFileToDropbox(string filePath) | |
{ | |
RestClient client = new RestClient("https://api-content.dropbox.com/1/"); | |
IRestRequest request = new RestRequest("files_put/auto/{path}", Method.PUT); | |
FileInfo fileInfo = new FileInfo(filePath); | |
long fileLength = fileInfo.Length; | |
request.AddHeader("Authorization", "Bearer INSERT_DEVELOPER_TOKEN_HERE"); | |
request.AddHeader("Content-Length", fileLength.ToString()); | |
request.AddUrlSegment("path", string.Format("Public/{0}", fileInfo.Name)); | |
byte[] data = File.ReadAllBytes(filePath); | |
var body = new Parameter | |
{ | |
Name = "file", | |
Value = data, | |
Type = ParameterType.RequestBody, | |
}; | |
request.Parameters.Add(body); | |
IRestResponse response = client.Execute(request); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RestSharp's
.AddFile()
method attaches a file as a multipart form POST, but the current Dropbox API requires that the body of the Request only be the file data, with other information passed in the query string. (And.AddBody()
appears to simply call.ToString()
when given anobject
.)This code uploads a file to a
Public
folder on your Dropbox account. It's based on the solution to this StackOverflow question.