0

Hi I want to upload file in box using box api 2.0 with ASp.net (4.0), c#. I am getting 400 (bad request error).

Following is my code.

`public bool Upload(byte[] FileBytes, string folderId, string FileName) { UploadFileBytes = FileBytes; UploadFileName = FileName;

    UploadSplitFile(folderId);
    return true;

}


public void UploadSplitFile(string FolderId)
{
    string Filename = UploadFileName;
    byte[] buffer;
    using (MemoryStream resultStream = new MemoryStream())
    {
        buffer = AssembleFilesBlock();
        resultStream.Write(buffer, 0, buffer.Length);
        buffer = GetFormattedBoundary(true);
        resultStream.Write(buffer, 0, buffer.Length);
        resultStream.Flush();
        buffer = resultStream.ToArray();
    }
    HttpWebRequest myRequest = CreateRequest(buffer.Length, FolderId);
    using (Stream newStream = myRequest.GetRequestStream())
    {
        newStream.Write(buffer, 0, buffer.Length);
        newStream.Close();
    }
    string response;

    using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse())
    {
        using (Stream responseStream = myHttpWebResponse.GetResponseStream())
        {
            TextReader reader = new StreamReader(responseStream);

            response = reader.ReadToEnd();
        }
    }
}
private byte[] AssembleFile()
{
    byte[] buffer;
    using (MemoryStream resultStream = new MemoryStream())
    {
        buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; parent_id=\"0\"{2}", Guid.NewGuid(), Path.GetFileName(UploadFileName), Environment.NewLine));
        resultStream.Write(buffer, 0, buffer.Length);
        buffer = Encoding.ASCII.GetBytes("Content-Type: application/octet-stream" + Environment.NewLine + Environment.NewLine);
        resultStream.Write(buffer, 0, buffer.Length);            
        buffer = UploadFileBytes;
        resultStream.Write(buffer, 0, buffer.Length);
        buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
        resultStream.Write(buffer, 0, buffer.Length);
        resultStream.Flush();
        buffer = resultStream.ToArray();
    }
    return buffer;
}

private byte[] AssembleFilesBlock()
{
    byte[] buffer;

    using (MemoryStream resultStream = new MemoryStream())
    {
        buffer = GetFormattedBoundary(false);
        resultStream.Write(buffer, 0, buffer.Length);

        buffer = AssembleFile();
        resultStream.Write(buffer, 0, buffer.Length);

        resultStream.Flush();
        buffer = resultStream.ToArray();
    }

    return buffer;
}

private byte[] GetFormattedBoundary(bool isEndBoundary)
{
    string template = isEndBoundary ? "--{0}--{1}" : "--{0}{1}";
    return Encoding.ASCII.GetBytes(string.Format(template, "AaBbCcX30", Environment.NewLine));
}

private HttpWebRequest CreateRequest(int contentLength, string FolderId)
{
    HttpWebRequest webRequest;
    webRequest = (HttpWebRequest)WebRequest.Create("https://upload.box.com/api/2.0/files/content");

    webRequest.Method = "POST";
    webRequest.AllowWriteStreamBuffering = true;
    webRequest.ContentType = string.Concat("multipart/form-data;boundary=", "AaBbCcX30");
    webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
    webRequest.Headers.Add("Accept-Charset", "ISO-8859-1");
    webRequest.Headers.Add("Authorization", "Bearer Access Token");
    webRequest.ContentLength = contentLength;
    return webRequest;
}`
1
  • Box has developed a .Net SDK that handles this kind of stuff for you. You might consider trying it out. On GitHub and Nuget. Commented Oct 7, 2013 at 20:05

1 Answer 1

1

Try this code.

I haven't used John Hoerr's SDK, it only uses RestSharp dll and the upload api which is given in http://developers.box.com/docs/#files-upload-a-file.

The parameter folder_id is the id of the folder to which you want to upload the file.

public void Upload_Doc(string folder_id,string accessToken)
        {
            var client = new RestClient("https://upload.box.com/api/2.0");
            var request = new RestRequest("files/content", Method.POST);
            request.AddParameter("parent_id", folder_id); 

            request.AddHeader("Authorization", "Bearer " + accessToken);

            string path = @"C:\Users\xyz\Desktop\homepage.png";
            byte[] byteArray = System.IO.File.ReadAllBytes(path);

            request.AddFile("filename", byteArray, "homepage.png");

            var responses = client.Execute(request);
            var content = responses.Content; 
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.