Box File上传未设置content_created_at属性c#

本文关键字:created at 属性 content 设置 File Box | 更新日期: 2023-09-27 18:22:42

我希望在上传文件时能够设置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 的网络请求

    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--

Box File上传未设置content_created_at属性c#

未传入content_created_at日期的原因是请求的关闭边界在请求中使用得太早。错误在此代码块中:

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

关闭的--AaB03x--边界停止在那里输入数据,并且在处理请求时不考虑它下面的行。删除这行代码将传递请求中的其余信息,并允许您设置自定义content_created_at日期。