简体   繁体   English

通过POST调用使用C#,RestSharp,Redmine API上传文件

[英]Upload file via POST call with C#, RestSharp, Redmine API

I'm developing a C# app that uses Redmine REST API, it uses RestSharp Client. 我正在开发使用Redmine REST API的C#应用​​程序,它使用RestSharp Client。 All other REST calls I make work fine but I cannot find a way to upload attachments. 我可以正常进行所有其他REST调用,但是找不到上载附件的方法。 I've widely searched the web and tried many solutions but nothing worked. 我在网络上进行了广泛搜索,并尝试了许多解决方案,但没有任何效果。 Redmine documentiation on attachments: http://www.redmine.org/projects/redmine/wiki/Rest_api#Attaching-files The code actually looks like: 附件上的Redmine文档: http : //www.redmine.org/projects/redmine/wiki/Rest_api#Attaching-files该代码实际上如下所示:

RestClient client = new RestClient("http://myclient/redmine/");
client.Authenticator = new HttpBasicAuthenticator("myuser", "mypsw");
var request2 = new RestRequest("uploads.json", Method.POST);
request2.AddHeader("Content-Type", "application/octet-stream");
request2.RequestFormat = RestSharp.DataFormat.Json;
byte[] dataToSend = File.ReadAllBytes(AddIssue.attach.Text);
request2.AddBody(dataToSend);
IRestResponse response2 = client.Execute(request2);
resultbox.Text = response2.Content;

If I execute it above nothing happens and the response is empty. 如果我执行它上面没有任何反应,并且响应为空。 If I remove line 7 (the AddBody), it actually works but of course nothing is uploaded, JSON response: { "upload": { "token": "11." 如果我删除了第7行(AddBody),它实际上可以工作,但当然没有任何上传,JSON响应:{“ upload”:{“ token”:“ 11”。 } } }}

So actually, the real question is what to punt in AddBody() to send the file as application/octet-stream. 因此,实际上,真正的问题是在AddBody()中添加什么以将文件作为application / octet-stream发送。 Since RestSharp also has a request.AddFile() method, I tried it too in different ways but nothing... 由于RestSharp也具有request.AddFile()方法,因此我也以其他方式尝试了它,但是没有任何办法...

Any help much appreciated! 任何帮助,不胜感激!

As I mentioned in my comment, it sounds like Redmine might have requirements similar to Dropbox. 正如我在评论中提到的那样,听起来Redmine可能具有与Dropbox类似的要求。 Here is the solution that worked for me (based on the question Upload to dropbox using Restsharp PCL ): 这是对我有用的解决方案(基于使用Restsharp PCL上载到Dropbox的问题):

public static void UploadFileToDropbox(string filePath)
{
    RestClient client = new RestClient("https://api-content.dropbox.com/1/");
    IRestRequest request = new RestRequest("files_put/auto/{path}", Method.PUT);

    FileInfo fileInfo = new FileInfo(filePath);
    long fileLength = fileInfo.Length;

    request.AddHeader("Authorization", "Bearer INSERT_DEVELOPER_TOKEN_HERE");
    request.AddHeader("Content-Length", fileLength.ToString());

    request.AddUrlSegment("path", string.Format("Public/{0}", fileInfo.Name));

    byte[] data = File.ReadAllBytes(filePath);
    var body = new Parameter
    {
        Name = "file",
        Value = data,
        Type = ParameterType.RequestBody,
    };
    request.Parameters.Add(body);

    IRestResponse response = client.Execute(request);
} 

Also published as a Gist . 以要点出版

I know this isn't your exact situation, but hopefully it gives you some ideas. 我知道这不是您的确切情况,但希望它能给您一些想法。

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

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