使用C#中的帖子请求上传到box.net

时间:2012-07-02 10:44:11

标签: c# asp.net httpwebrequest box-api

我想通过api在box.net上传文件,我通过邮寄请求以这种方式发布我的文件

根据Box.NET的文档,这里是请求网址

https://upload.box.net/api/1.0/upload/<auth token>/<folder id>

这是文档http://developers.box.net/w/page/12923951/ApiFunction_Upload%20and%20Download

WebRequest request = WebRequest.Create("https://upload.box.net/api/1.0/upload...;
request.Method = "POST";

byte[] byteArray = File.ReadAllBytes(@"C:\a.docx");

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close(); 

我收到此消息“upload_no_files_found”

的异常

3 个答案:

答案 0 :(得分:1)

根据示例上传请求中的this page ,Box.net会请求一些参数,例如new_file1shareemails[]

因此,您需要发送此参数,而不仅仅是文件。从MSDN sample for how to post parametres开始,关键点是使用您要发送的文件创建此完整字符串。我专注于这一行,使用要发送的文件制作参数。

    string postData = "firstone=" + inputData;
    ASCIIEncoding encoding = new ASCIIEncoding ();
    byte[] byte1 = encoding.GetBytes (postData);

对于您的情况,这看起来像

    string postData = "share=1&emails[]=test@domain.com&new_file1=" + FileData;
    ASCIIEncoding encoding = new ASCIIEncoding ();
    byte[] byte1 = encoding.GetBytes (postData);

,您的最终代码必须如下:

     WebRequest request = WebRequest.Create("https://upload.box.net/api/1.0/<auth_token>/<file_id>/<version_id>;
     request.Method = "POST";

    // open and read file
    byte[] byteArray = File.ReadAllBytes(@"C:\a.docx");

    // make the parametres
    string postData = "share=1&emails[]=test@domain.com&new_file1=";
    ASCIIEncoding encoding = new ASCIIEncoding ();
    byte[] parametres = encoding.GetBytes (postData);

    // set the Type
    request.ContentType = "application/x-www-form-urlencoded";
    // the full length.
    request.ContentLength = parametres.Length + byteArray.Length;

    // now we go for post
    Stream dataStream = request.GetRequestStream();
    // send the parametres
    dataStream.Write(parametres, 0, parametres.Length);
    // follow the file
    dataStream.Write(byteArray, 0, byteArray.Length);

    // flush and close what you have send  
    dataStream.Close();

现在这是个想法,但是我无法调试这个代码nether测试它,可能不是第一个工作,需要一些更改和调试。

答案 1 :(得分:0)

这可能会有所帮助。我正在使用v2 API和RestSharp。

    // as per http://developers.box.com/docs/#files-upload-a-file
        RestClient client1 = new RestClient();
        client1.BaseUrl = "https://upload.box.com";
        var request = new RestRequest(Method.POST);
        request.Resource = "api/2.0/files/data";
        string Headers = string.Format("BoxAuth api_key={0}&auth_token={1}",
                api_key,
                AuthToken);
        request.AddHeader("Authorization", Headers);
        request.AddParameter("folder_id", FolderID);
        request.AddFile("filename", filePath);

        //request.RequestFormat = DataFormat.Json;
        var response = client1.Execute(request);
        return response.Content;

答案 2 :(得分:0)

创建文件夹并上传文档VIA Box.net和BOX API

public void Upload_Doc(string folder_id, string accessToken)
    {
   var client = new RestClient("https://upload.box.com/api/2.0");
    var request = new RestRequest("files/content", Method.POST);
    request.AddParameter("parent_id", folder_id);

    request.AddHeader("Authorization", "Bearer " + accessToken);

    string path = @"D:\Project\21Teach\Documents\screenshots.docx";
    byte[] byteArray = System.IO.File.ReadAllBytes(path);

    request.AddFile("filename", byteArray, "screenshots.docx");

    var responses = client.Execute(request);
    var content = responses.Content;
}
static string folderCreation(string APIKey, string authToken)
{

    RestClient client = new RestClient();
    client.BaseUrl = "https://api.box.com/2.0/folders";
    var request = new RestRequest(Method.POST);
    string Headers = string.Format("Bearer {0}", authToken);
    request.AddHeader("Authorization", Headers);
    request.AddParameter("application/json", "{\"name\":\"Youka\",\"parent\":{\"id\":\"0\"}}", ParameterType.RequestBody);
    var response = client.Execute(request);
    return response.Content;



}