2017-01-19 51 views
0

我想上传使用Zendesk API v2的图像,我使用RestSharp发布文件到/api/v2/uploads.json,并且该文件出现在一旦我创建了票证并添加附件,问题是如果我上传的图像不会在票证上打开,那么它会被破坏,如果它的.txt文件中有额外的数据,这是我的方法:错误,同时尝试上传文件到Zendesk Api 2在c#

var client = new RestSharp.RestClient(model.RequestUri); 
client.Authenticator = new HttpBasicAuthenticator(string.Format("{0}/token", model.Username), model.Password); 

var request = new RestRequest("/api/v2/uploads.json", Method.POST); 
request.AddHeader("Accept", "application/json"); 
request.AddHeader("Content-Type", "text/plain"); 
request.AlwaysMultipartFormData = true; 
request.Parameters.Clear(); 

request.RequestFormat = RestSharp.DataFormat.Json; 
//request.AddBody(createUpload); 
byte[] bytes = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Media/uploads/test.txt")); 
request.AddFileBytes("Attachment", bytes, "test.txt", contentType: "text/plain"); 
request.AddParameter("filename", "test.txt"); 

IRestResponse response = client.Execute(request); 
var content = JObject.Parse(response.Content); 
return content["upload"]["token"].ToString(); 

这是附加到票所产生的txt文件:

-------------------------------28947758029299 
Content-Disposition: form-data; name="filename" 

test.txt 
-------------------------------28947758029299 
Content-Disposition: form-data; name="Attachment"; filename="test.txt" 
Content-Type: application/octet-stream 

testing txt 
-------------------------------28947758029299-- 

原始文件只是有:

testing txt 

任何想法的错误可能是什么?

谢谢。

+0

你好@LeoTorres,我有同样的问题。你有没有得到它的修复。任何解决方案,而不使用JustEat的API –

回答

1

我解决那是的Zendesk文档中推荐使用称为ZendeskApi外部库问题:https://github.com/justeat/ZendeskApiClient

通过使用这个库,我能够成功上传附件,并且它可以处理任何类型的文件。它也很容易使用,我的方法现在看起来是这样的:

IZendeskClient client = new ZendeskClient(
    new Uri(model.RequestUri), 
    new ZendeskDefaultConfiguration(model.Username, 
      model.Password) 
); 

UploadRequest request = new UploadRequest() { 
    Item = model.Attachment.ConvertToZendeskFile() 
}; 

IResponse<Upload> response = client.Upload.Post(request); 
return response.Item.Token; 

这是ConvertToZendeskFile方法:

private static ZendeskFile ConvertToZendeskFile(this HttpPostedFileBase rawFile) 
{ 
    return new ZendeskFile() 
    { 
     FileName = rawFile.FileName, 
     ContentType = rawFile.ContentType, 
     InputStream = rawFile.InputStream 
    }; 
} 

的最后一个步骤是创建一个从API实现IHttpPostedFile类:

public class ZendeskFile : IHttpPostedFile 
{ 
    public string ContentType { get; set; } 
    public string FileName { get; set; } 
    public Stream InputStream { get; set; } 
} 

这解决了我的问题,我希望它可以帮助任何人面临同样的问题。

0

我已经成功上传使用代码的图像和PDF文件到的Zendesk片断与此类似:

var client = new RestClient(apiUrl); 
client.Authenticator = new HttpBasicAuthenticator(username + "/token", token); 
client.AddDefaultHeader("Accept", "application/json"); 

string name = "name"; 
byte[] data; //Read all bytes of file 
string filename = "filename.jpg"; 

var request = new RestRequest("/uploads.json", Method.POST); 
request.AddFile(name, data, filename, "application/binary"); 
request.AddQueryParameter("filename", filename); 
var response = client.Execute(request); 
+0

嗨,感谢您的答案,我实现了这一点,我不能上传图像,但txt文件更改为:----------------- -------------- 28947758029299 Content-Disposition:form-data; NAME = “名称”; filename =“test.txt” Content-Type:application/binary testing txt ------------------------------ -28947758029299-- – LeoTorres

相关问题