如何在 VSTS rest 的帮助下将 Zip 文件上传到 VSO workitem api
How to upload Zip files to VSO workitem with the help of VSTS rest api
String Filepath = Txt_Attachfile.Text;
string accesToken = ConfigurationManager.AppSettings["AccessToken"];
var u = new Uri("https://abc.visualstudio.com/");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, accesToken));
var connection = new VssConnection(u, c);
var workItemTracking = connection.GetClient<WorkItemTrackingHttpClient>();
string patchcontent = "";
JsonPatchDocument patchDocument = new JsonPatchDocument();
if (Txt_Attachfile.Text != "")
{
string filename = Path.GetFileName(Txt_Attachfile.Text);
Attachemt attachment = new Attachemt();
attachment = UpdloadToStore(filename);
patchDocument.Add(new JsonPatchOperation()
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachment.url,
attributes = new
{
comment = "VanDelay Industries - Spec"
}
}
});
var result = workItemTracking.UpdateWorkItemAsync(patchDocument, Convert.ToInt32(workitem));
patchcontent = JsonConvert.SerializeObject(result);
}
对于附件上传限制较高 (>130MB) 的帐户,您将需要使用“分块”上传来上传您的文件。首先,通过执行以下操作注册您的分块上传:
由于添加工作项附件的文件是 10MB (<130MB),您应该使用 REST API 作为 upload a text file instead of chunked upload REST API.
以及将文件上传到 VSTS 并将文件添加为工作项附件的示例代码,如下所示:
int id=12;
string filename = @"C:\path\to\the\upload\file";
Uri accountUri = new Uri("https://account.visualstudio.com");
String personalAccessToken = "PAT";
VssConnection connection1 = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
Console.WriteLine("Attempting upload of: {0}", "");
AttachmentReference attachment = workItemTrackingClient.CreateAttachmentAsync(filename).Result;
Console.WriteLine(attachment.Id);
Console.WriteLine(attachment.Url);
BuildHttpClient buildClient = connection.GetClient<BuildHttpClient>();
WorkItemTrackingHttpClient workItemTrackingClient1 = connection.GetClient<WorkItemTrackingHttpClient>();
JsonPatchDocument patchDocument = new JsonPatchDocument
{
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachment.Url,
attributes = new { comment = "VanDelay Industries - Spec" }
}
}
};
WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, id).Result;
Console.WriteLine("succeed!");
String Filepath = Txt_Attachfile.Text;
string accesToken = ConfigurationManager.AppSettings["AccessToken"];
var u = new Uri("https://abc.visualstudio.com/");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, accesToken));
var connection = new VssConnection(u, c);
var workItemTracking = connection.GetClient<WorkItemTrackingHttpClient>();
string patchcontent = "";
JsonPatchDocument patchDocument = new JsonPatchDocument();
if (Txt_Attachfile.Text != "")
{
string filename = Path.GetFileName(Txt_Attachfile.Text);
Attachemt attachment = new Attachemt();
attachment = UpdloadToStore(filename);
patchDocument.Add(new JsonPatchOperation()
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachment.url,
attributes = new
{
comment = "VanDelay Industries - Spec"
}
}
});
var result = workItemTracking.UpdateWorkItemAsync(patchDocument, Convert.ToInt32(workitem));
patchcontent = JsonConvert.SerializeObject(result);
}
对于附件上传限制较高 (>130MB) 的帐户,您将需要使用“分块”上传来上传您的文件。首先,通过执行以下操作注册您的分块上传:
由于添加工作项附件的文件是 10MB (<130MB),您应该使用 REST API 作为 upload a text file instead of chunked upload REST API.
以及将文件上传到 VSTS 并将文件添加为工作项附件的示例代码,如下所示:
int id=12;
string filename = @"C:\path\to\the\upload\file";
Uri accountUri = new Uri("https://account.visualstudio.com");
String personalAccessToken = "PAT";
VssConnection connection1 = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
Console.WriteLine("Attempting upload of: {0}", "");
AttachmentReference attachment = workItemTrackingClient.CreateAttachmentAsync(filename).Result;
Console.WriteLine(attachment.Id);
Console.WriteLine(attachment.Url);
BuildHttpClient buildClient = connection.GetClient<BuildHttpClient>();
WorkItemTrackingHttpClient workItemTrackingClient1 = connection.GetClient<WorkItemTrackingHttpClient>();
JsonPatchDocument patchDocument = new JsonPatchDocument
{
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachment.Url,
attributes = new { comment = "VanDelay Industries - Spec" }
}
}
};
WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, id).Result;
Console.WriteLine("succeed!");