箱文件上传未设置content_created_at属性C# [英] Box File upload not setting content_created_at attribute c#

查看:172
本文介绍了箱文件上传未设置content_created_at属性C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够设置当我上传的文件,由于某种原因,我不能把它设置content_created_at属性。该文件上传就好了正确的位置,正确的名称,但content_created_at不被设置。有什么想法?



下面是上传文件的代码。

 
{
// URL建立
串服务=https://upload.box.com/api/2.0/files/;
串strContent =内容;
串urlStr =服务+ strContent;

//文件和表单数据一起
// - 因为我们的基础是.NET 4中,我们必须手动放在一起请求。它的丑陋,但它的工作原理。
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(urlStr);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method =POST;
request.Headers.Add(授权,承载+的accessToken);
串边界=AaB03x;
request.ContentType =的multipart / form-data的;边界=+边界;
request.AllowWriteStreamBuffering = FALSE;

//创建后的数据
串lineEnd =\r\\\
;
串twoHyphens = - ;
字节[]缓冲区;
流requestStream = NULL;

弦线1 = twoHyphens +边界+ lineEnd;
串2号线=内容处置:表格数据;名称= \filename\; +=文件名\+姓名+\+ lineEnd;
线3号线= lineEnd;
线4号线= twoHyphens +边界+ twoHyphens + lineEnd;
串LINE5 =(内容类型:内容/流;+ lineEnd);
串LINE6 =内容处置:表格数据;名称= \folder_id\+ lineEnd + lineEnd ++身份证+;
串line7 =内容处置:表格数据;名称= \content_created_at\+ lineEnd + lineEnd ++ checkCorrectDateTimeFormat(DateTime.Now.AddDays(-1)的ToString())+
串postHeader =一号线2号线+ + + LINE5 3号线;
字节[] = postHeaderBytes Encoding.UTF8.GetBytes(postHeader);
字节[] = boundaryBytes Encoding.ASCII.GetBytes(3号线+ 1行+ LINE6 + 3号线+ 4号线);
字节[] = contentDateBytes Encoding.ASCII.GetBytes(3号线+ 1行+ line7 + 3号线+ 4号线);
长度长= postHeaderBytes.Length + cData.Length + boundaryBytes.Length + contentDateBytes.Length;
request.ContentLength =长度;使用(requestStream = request.GetRequestStream())
{

//写出我们的后头部
requestStream.Write(postHeaderBytes,0,postHeaderBytes.Length);

//写出文件的内容
缓冲区=新的字节[检查((长)Math.Min(1024 * 512(长)cData.Length))];
INT读取动作= 0;
而((读取动作= cData.Read(缓冲液,0,buffer.Length))!= 0)
requestStream.Write(缓冲液,0,读取动作);

//写出尾部边界
requestStream.Write(boundaryBytes,0,boundaryBytes.Length);
requestStream.Write(contentDateBytes,0,contentDateBytes.Length);
}

//利用发送数据
(HttpWebResponse响应= request.GetResponse()作为HttpWebResponse)
{
如果(response.StatusCode!= HttpStatusCode.Created)
抛出新的异常(的String.Format(
服务器错误(HTTP {0}:{1}),
response.StatusCode,
响应。状态说明));

//解析JSON响应
DataContractJsonSerializer jsonSerializer =新DataContractJsonSerializer(typeof运算(BoxCommon.FolderItems));
BoxCommon.FolderItems objResponse = jsonSerializer.ReadObject(response.GetResponseStream())作为BoxCommon.FolderItems;
返回objResponse.entries [0] .ID;
}
}
赶上(异常前)
{
err.Append(ex.Message);
}

这是从提琴手


$ B将向WebRequest $ b

  POST https://upload.box.com/api/2.0/files/content HTTP / 1.1 
授权:承载的accessToken
的Content-Type :的multipart / form-data的;边界= AaB03x
主机:upload.box.com
的Content-Length:20099
期望:100-继续
连接:保持活动

--AaB03x
内容处置:表格数据; NAME =文件名;文件名=testdoc.docx
内容类型:内容/流;
FILEDATA
--AaB03x
内容处置:表格数据; NAME =folder_id

5459960629
--AaB03x--

--AaB03x

内容处置:表格数据; NAME =content_created_at

2015-11-18T20:07:08Z

--AaB03x--


解决方案

为什么 content_created_at 日期是不是在传递的原因是由于请求的结束边界正在使用的请求为时尚早。该错误是在这个代码块:

  --AaB03x 
内容处置:表格数据; NAME =folder_id

5459960629
--AaB03x--

闭幕 - AaB03x - 边界停止数据输入那里,它下面的线条处理请求时不予以考虑。删除该行代码将通过与请求的其余信息,并允许您设置自定义的 content_created_at 日期。


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

解决方案

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.

这篇关于箱文件上传未设置content_created_at属性C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆