无法使用正确的文件名和扩展名在devops rest api中下载Attachemet [英] Not able to download attachemet in devops rest api with proper filename and extension

查看:46
本文介绍了无法使用正确的文件名和扩展名在devops rest api中下载Attachemet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用REST API从devops下载工作项附件.该文件正在下载中,但是文件名和扩展名不正确,下载后我无法打开.

I was trying to download work item attachment from devops using the REST API. The file is downloading, but file name and extension are not correct and I'm unable to open after downloading the file.

var personalaccesstoken = "";
var DeserializedClass = new List<Responcejson>();
string bseurl = "https://xxx/_apis/wit/attachments/xxxx-0cdb-4f53-9785-55d642d603e7?fileName=abc.png&download=true&api-version=5.0";

try
{
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        System.Text.ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "", personalaccesstoken))));

        using (HttpResponseMessage response = await client.GetAsync(bseurl))
        {
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            //JObject data = JObject.Parse(responseBody);
            return responseBody;
        }
    }
}
catch (Exception ex)
{
    return ex.ToString();
}

,但这会将文件下载为"Devops",没有文件名和扩展名....

but this downloads the file as "Devops" with no filename and extension....

预先感谢

推荐答案

在执行Azure DevOps REST API时<应用程序/八位字节流"或"应用程序/邮政编码".请参阅

When executing the Azure DevOps REST API "Downloads an attachment", you can use the Media Types "application/octet-stream" or "application/zip". See here.

我在您的代码中注意到了这一点,

I noticed this in your code,

string responseBody =等待响应.Content.ReadAsStringAsync();

string responseBody = await response.Content.ReadAsStringAsync();

如果附件是图片,则可能会导致问题.

If the attachment is image, this may cause issues.

此外,您可以在C#代码中直接使用相应的客户端API.请参见"

In addition, there is the corresponding client API you can directly use in your C# code. See "WorkItemTrackingHttpClientBase.GetAttachmentContentAsync Method".

这篇关于无法使用正确的文件名和扩展名在devops rest api中下载Attachemet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆