2012-07-02 39 views
3

我想通过API将其上传于box.net文件,我张贴通过POST请求我的文件中这样上传到box.net C#中的POST请求

根据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”

+0

看起来你已经做了从MSDN的所有步骤,但是这行'Console.WriteLine'与ASP做.net? – Aristos

+0

是啊aristos ..我正在测试不同的事情。我正在写调试。它的错误... –

+0

其实我们正在传递具有所有信息的authtoken。其他的事情是文件夹名称在哪里张贴和发布我的文件流到该API。 –

回答

1

根据Sample Upload请求中的this page,Box.net请求一些参数,如new_file1,shareemails[]

所以你需要发送这个参数,而不仅仅是文件。从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[][email protected]&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[][email protected]&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(); 

现在,这是想法,但我不能调试代码幽冥测试它,可能不会像第一次那样工作,并且需要进行一些更改和调试。

+0

谢谢。让我测试一下。我会告诉你在未来5分钟 –

+0

它不工作。返回错误是“找不到页面” –

+0

@ArslanPervaiz我写的文章很难马上工作,我也无法调试,但这是您需要遵循的行来找到解决方案。 – Aristos

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



}