Box 文件上传未设置 content_created_at 属性 c#
Box File upload not setting content_created_at attribute c#
我希望能够在上传文件时设置 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--
content_created_at
日期没有传入的原因是请求的关闭边界在请求中使用的太早了。错误在此代码块中:
--AaB03x
Content-Disposition: form-data; name="folder_id"
5459960629
--AaB03x--
关闭--AaB03x--
边界停止在那里输入数据,处理请求时不考虑它下面的行。删除该行代码将通过请求传递其余信息,并允许您设置自定义 content_created_at
日期。
我希望能够在上传文件时设置 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--
content_created_at
日期没有传入的原因是请求的关闭边界在请求中使用的太早了。错误在此代码块中:
--AaB03x
Content-Disposition: form-data; name="folder_id"
5459960629
--AaB03x--
关闭--AaB03x--
边界停止在那里输入数据,处理请求时不考虑它下面的行。删除该行代码将通过请求传递其余信息,并允许您设置自定义 content_created_at
日期。