我正在尝试使用 Zendesk API v2 上传图像,我正在使用 RestSharp 将文件发布到 /api/v2/uploads.json,一旦创建票证并添加附件,该文件就会出现在票证中,问题是如果我上传一张图片,它不会在票证上打开,它就坏了,如果它是一个 .txt 文件,它那里有额外的数据,这是我的方法:
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
关于错误可能是什么的任何想法?
谢谢。