Box File upload not setting content_created_at att

2019-07-09 07:16发布

问题:

I want to be able to set the content_created_at attribute when I upload a file and for some reason I can't get it to set. The file uploads just fine to the correct location, correct name, but the content_created_at doesn't get set. Any thoughts?

Here is the code that is uploading the file.

      try
        {
            // Build URL
            string service = "https://upload.box.com/api/2.0/files/";
            string strContent = "content";
            string urlStr = service + strContent;

            // File and form data together
            //  - Because our base is .NET 4, we have to manually put together the request.  It's ugly but it works.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "POST";
            request.Headers.Add("Authorization", "Bearer " + accessToken);
            string boundary = "AaB03x";
            request.ContentType = "multipart/form-data;boundary=" + boundary;
            request.AllowWriteStreamBuffering = false;

            // Create post data   
            string lineEnd = "\r\n";
            string twoHyphens = "--";
            byte[] buffer;
            Stream requestStream = null;

            string line1 = twoHyphens + boundary + lineEnd;
            string line2 = "Content-Disposition: form-data; name=\"filename\";" + " filename=\"" + name + "\""  + lineEnd;
            string line3 = lineEnd;
            string line4 = twoHyphens + boundary + twoHyphens + lineEnd;
            string line5 = ("Content-Type: content/stream;" + lineEnd);
            string line6 = "Content-Disposition: form-data; name=\"folder_id\"" + lineEnd + lineEnd + "" + id + "";
            string line7 = "Content-Disposition: form-data; name=\"content_created_at\"" +lineEnd+ lineEnd+"" + checkCorrectDateTimeFormat(DateTime.Now.AddDays(-1).ToString())  + "";
            string postHeader = line1 + line2 + line5 + line3;
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
            byte[] boundaryBytes = Encoding.ASCII.GetBytes(line3 + line1 + line6 + line3 + line4);
            byte[] contentDateBytes = Encoding.ASCII.GetBytes(line3 + line1 + line7 + line3 + line4);
            long length = postHeaderBytes.Length + cData.Length + boundaryBytes.Length+contentDateBytes.Length;
            request.ContentLength = length;
            using (requestStream = request.GetRequestStream())
            {
                // Write out our post header
                requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                // Write out the file contents
                buffer = new Byte[checked((long)Math.Min(1024 * 512, (long)cData.Length))];
                int bytesRead = 0;
                while ((bytesRead = cData.Read(buffer, 0, buffer.Length)) != 0)
                    requestStream.Write(buffer, 0, bytesRead);

                // Write out the trailing boundary
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                requestStream.Write(contentDateBytes, 0, contentDateBytes.Length);
            }              

            // Send data
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.Created)
                    throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));

                // Parse the JSON response 
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(BoxCommon.FolderItems));
                BoxCommon.FolderItems objResponse = jsonSerializer.ReadObject(response.GetResponseStream()) as BoxCommon.FolderItems;
                return objResponse.entries[0].id;
            }
        }
        catch (Exception ex)
        {
            err.Append(ex.Message);
        }

this is the webRequest from Fiddler

    POST https://upload.box.com/api/2.0/files/content HTTP/1.1
    Authorization: Bearer AccessToken
    Content-Type: multipart/form-data;boundary=AaB03x
    Host: upload.box.com
    Content-Length: 20099
    Expect: 100-continue
    Connection: Keep-Alive

    --AaB03x
    Content-Disposition: form-data; name="filename"; filename="testdoc.docx"
    Content-Type: content/stream;
    FILEDATA                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    --AaB03x
    Content-Disposition: form-data; name="folder_id"

    5459960629
    --AaB03x--

    --AaB03x

    Content-Disposition: form-data; name="content_created_at"

    2015-11-18T20:07:08Z

    --AaB03x--

回答1:

The reason why the content_created_at date was not passed in is due to the closing boundary for the request being used too early in the request. The error is in this code block:

 --AaB03x
Content-Disposition: form-data; name="folder_id"

5459960629
--AaB03x--

The closing --AaB03x-- boundary stops the data input there, and the lines below it are not considered when processing the request. Removing that line of code will pass the rest of the information with the request, and allow you to set a custom content_created_at date.



标签: c# box-api