Reputation: 35
I'm trying to use Box API to upload files. I've done it in cURL with the example,
curl https://upload.box.com/api/2.0/files/content -k -H
"Authorization: Bearer (access token)" -F filename=@"File path and name" -F folder_id=3015577881
I can upload my file, and see it just fine in the folder that I direct it to. I want to be able to do this from a C# console application.
var url = "https://upload.box.com/api/2.0/files/content";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "multipart/form-data";
req.Headers.Add("Authorization", string.Format("Bearer {0}", accessToken));
var outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
outgoingQueryString.Add("filename", String.Format("@{0}",fileName));
outgoingQueryString.Add("folder_id", folderId);
string postData = outgoingQueryString.ToString();
var ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData);
var postStream = req.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
req.GetResponse();
With this, I get error 500. When I try to do it without the content type, I get error 405 (method_not_allowed). I've tried other content-types and they also give me 405. What am I doing wrong?
Upvotes: 0
Views: 659
Reputation: 8035
Using the Box .Net client, you can upload a new file like this:
BoxFileRequest req = new BoxFileRequest()
{
Name = "NewFile",
Parent = new BoxRequestEntity() { Id = "0" }
};
using (var stream = System.IO.File.Open(@"c:\path\to\file.txt", FileMode.Open))
{
BoxFile f = await client.FilesManager.UploadAsync(request, stream);
}
Upvotes: 1