2

我正在尝试使用 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

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

谢谢。

4

5 回答 5

2

我使用 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; }
}

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

于 2017-01-20T08:33:40.133 回答
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);
于 2017-01-19T11:38:34.163 回答
0

需要ContentType=application/binary在 URI 中添加标头并提供文件名?filename=myfile.dat

HttpClient client = [...];
var content = new ByteArrayContent(fileByteArray);
content.Headers.ContentType = new MediaTypeHeaderValue("application/binary");
HttpResponseMessage response = await client.PostAsync(url, content);
string responseString = await response.Content.ReadAsStringAsync();

来自 Zendesk 文档:

curl "https://{subdomain}.zendesk.com/api/v2/uploads.json?filename=myfile.dat&token={optional_token}" \
-v -u {email_address}:{password} \
-H "Content-Type: application/binary" \
--data-binary @file.dat -X POST
于 2018-08-14T12:06:00.447 回答
0

我遇到了同样的问题,Restsharp 将文件作为多部分发送,唯一对我有用的解决方案是将文件作为参数发送,内容为“应用程序/二进制”。

public string UploadFile(ZendeskFile file)
{
    try
    {
        var request = new RestRequest(FileUploadsPath, Method.POST);
        request.AddQueryParameter("filename", file.Name);
        request.AddParameter("application/binary", file.Data, ParameterType.RequestBody);
        var response = Execute<UploadZendeskFileResponse>(request);
        var result = JsonConvert.DeserializeObject<UploadZendeskFileResponse>(response.Content);
        return result.upload.token;
    }
    catch (Exception ex)
    {
        throw ex;
    }

}

我希望这对其他人有帮助。

于 2019-06-14T12:22:31.960 回答
0

就我而言,我做了这样的事情。希望你不会像我一样浪费6个小时!

public async Task UploadImageToZendesk(IFormFile image)
    {
        byte[] fileByteArray;
        var request = new HttpRequestMessage();
        var client = new HttpClient();

        await using (var fileStream = image.OpenReadStream())
        await using (var memoryStream = new MemoryStream())
        {
            await fileStream.CopyToAsync(memoryStream);
            fileByteArray = memoryStream.ToArray();
        }

        ByteArrayContent byteContent = new ByteArrayContent(fileByteArray);
        request.Content = byteContent;
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse($"application/binary");

        await client.SendAsync(request);
    }
于 2021-03-04T16:19:30.210 回答