Not able to download attachemet in devops rest api with proper filename and extension

浪子不回头ぞ 提交于 2020-12-15 04:29:00

问题


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();
}

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

Thanks in advance


回答1:


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 = await response.Content.ReadAsStringAsync();

If the attachment is image, this may cause issues.

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



来源:https://stackoverflow.com/questions/65182873/not-able-to-download-attachemet-in-devops-rest-api-with-proper-filename-and-exte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!