问题描述
我试图使用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,
如果附件是图片,则可能会导致问题.
If the attachment is image, this may cause issues.
此外,您可以在C#代码中直接使用相应的客户端API.请参见" WorkItemTrackingHttpClientBase.GetAttachmentContentAsync方法".
In addition, there is the corresponding client API you can directly use in your C# code.See "WorkItemTrackingHttpClientBase.GetAttachmentContentAsync Method".
这篇关于无法使用正确的文件名和扩展名在devops rest api中下载Attachemet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!