Box文件上传未设置content_created_at属性c#

时间:2015-11-20 05:56:30

标签: c# box-api

我希望能够在上传文件时设置content_created_at属性,由于某种原因我无法设置它。文件上传到正确的位置,正确的名称,但content_created_at没有设置。有什么想法吗?

以下是上传文件的代码。

      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);
        }

这是来自Fiddler的webRequest

    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 个答案:

答案 0 :(得分:0)

未传递<html lang="en" ng-app="app"> 日期的原因是由于在请求中过早使用请求的结束边界。错误发生在此代码块中:

content_created_at

关闭 --AaB03x Content-Disposition: form-data; name="folder_id" 5459960629 --AaB03x-- 边界会停止在那里输入数据,并且在处理请求时不会考虑它下面的行。删除该行代码将通过请求传递其余信息,并允许您设置自定义--AaB03x--日期。