3

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

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

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”的异常

4

3 回答 3

1

根据示例上传请求上的此页面,Box.net 请求一些参数new_file1,例如shareemails[]

因此,您需要发送此参数而不仅仅是文件。从MSDN 示例中了解如何发布参数的关键点是使用您要发送的文件制作这个完整的字符串。我专注于将参数与要发送的文件一起制作的这一行。

    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();

现在是这个想法,但我无法调试这段代码来测试它,也许从一开始就无法正常工作,需要进行一些更改和调试。

于 2012-07-02T11:14:33.000 回答
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;
于 2012-08-04T12:06:23.233 回答
0

通过 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;



}
于 2014-02-25T13:02:58.680 回答