无法使用正确的文件名和扩展名在 devops rest api 中下载附件

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

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

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”,没有文件名和扩展名....

提前致谢

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

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

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

如果附件是图片,这可能会导致问题。

此外,还有相应的客户端API,您可以直接在您的C#代码中使用。 参见“WorkItemTrackingHttpClientBase.GetAttachmentContentAsync Method”。