简体   繁体   English

我们如何使用C#中的API将文件上传到机架云容器中

[英]How we can Upload files into rackspace cloud container with API in C#

I want to Upload files into rackspace cloud container with API in C# and I am using .net 4.0 version. 我想使用C#中的API将文件上传到机架云容器中,并且正在使用.net 4.0版本。 So, how I can create webrequest for this. 因此,我如何为此创建webrequest。 Even I successfully created containers with the same request but I am not able to create object into my container. 即使我成功创建了具有相同请求的容器,但也无法在容器中创建对象。

Number of times I tried to upload my file into my container but I am continuously getting error like Unauthorized access and my code is shown below: 我尝试将文件上传到容器中的次数,但是我不断收到诸如未授权访问的错误,我的代码如下所示:

HttpWebRequest request = WebRequest.Create(new Uri(authInfo.StorageUrl + "/TestContainer/myfile.txt")) as HttpWebRequest;
request.Method = "PUT";
request.Headers["X-Auth-Token"] = MyToken;
byte[] data = System.IO.File.ReadAllBytes(@"D:\myfile.txt");
request.ContentLength = data.Length;
//request.Headers["Content-Length"] = "512000";
var response = request.GetResponse();

Please tell me what I am doing wrong with this. 请告诉我我在做什么错。

You haven't written the bytes to the request stream. 您尚未将字节写入请求流。 Do something like this: 做这样的事情:

Stream reqStream = request.GetRequestStream();

reqStream.Write(fileBytes, 0, fileBytes.Length);

reqStream.Close();

Webresponse response = request.getresponse();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM