使用API​​为Visual Studio Team Services添加附件(是Visual Studio Online) [英] Adding Attachment using API for Visual Studio Team Services (was Visual Studio Online)

查看:76
本文介绍了使用API​​为Visual Studio Team Services添加附件(是Visual Studio Online)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的winform应用程序,该应用程序可以使用提供的API在我的Visual Studio Team Services敏捷工作流程中创建新的错误项. API文档

I am in the process of creating a simple winform application, which can create new bug items into my Visual Studio Team Services agile workflow, using the API provided. The API Documentation

当前它可以创建一个带有标题,标签和描述的新错误.

Currently it can create a new bug, with title, tags and description.

我希望能够添加文件附件,但是由于某些原因,这无法正常工作.

I want to be able to add file attachments, but for some reason this is not working.

下面是创建错误的代码

    private static void createInitailItemPostObject()
    {                                           
        AddUpdateProp("/fields/System.Title", newTaskItem.Title);
        AddUpdateProp("/fields/System.Tags", newTaskItem.Tags);
        AddUpdateProp("/fields/System.Description", newTaskItem.Description);
        AddUpdateProp("/fields/System.History", "Upload first file");      
    }

    private static void AddUpdateProp( string field, string value)
    {            
        DataObjectsProject.VSOJasonWorkItemPostData wiPostData = new DataObjectsProject.VSOJasonWorkItemPostData();
        wiPostData.op = "add";
        wiPostData.path = field;
        wiPostData.value = value;
        wiPostDataArr.Add(wiPostData);           
    }

JSon调用使用以下代码完成

The JSon call is done with the below code

 public static async void Post(string url, System.Net.Http.HttpContent wiPostDataContent, string returnType, JSONReturnCallBack callBack)
    {
       string responseString = String.Empty;
       try
       {
            using (System.Net.Http.HttpClient client = new System.Net.Http.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}", username, password))));


                  using (System.Net.Http.HttpResponseMessage response = client.PostAsync(url, wiPostDataContent).Result)
                  {
                        response.EnsureSuccessStatusCode();
                        string ResponseContent = await response.Content.ReadAsStringAsync();

                        responseString = ResponseContent;

                  }
            }
        }
        catch(Exception ex)
        {
                        Console.WriteLine(ex.ToString());
                        Console.ReadLine();
        }
       callBack(responseString,returnType);
    }

要添加附件,我似乎无法将下面的代码转换为与当前代码一样的工作.

To add an attachment I can't seem to get it to convert the below code to work like my current code.

function readBlob() {
var files = document.getElementById('fileselect').files;
if (!files.length) {
    alert('Please select a file!');
    return;
}
var file = files[0];
var filename = file.name;
var reader = new FileReader();
reader.onloadend = function (evt) {
    if (evt.target.readyState == FileReader.DONE) {
        // Post file content to server
        $.ajax({
            url: "http://fabrikam.visualstudio.com/DefaultCollection/_apis/wit/attachments?filename=" + filename + "&api-version=1.0",
            data: evt.target.result,
            processData: false,
            contentType: "application/json",
            type: "POST"
        });
    }
};
reader.readAsArrayBuffer(file);

}

其他任何人都可以做到这一点吗?

Has anyone else been able to do this?

我正在使用的URL是以下用于调用API的URL

The URL I am using is the below url to call the API

" https ://[ACCOUNT] .visualstudio.com/DefaultCollection/[Project]/_apis/wit/attachments?fileName=Test&api-version=1.0 "

推荐答案

如示例代码所示,您无需将项目放在API调用的URL中.尝试使用以下网址进行操作:

As you can see in the example code, you don't need to put the project in the URL for the API call. Try doing it with the following url:

" https://[ ACCOUNT] .visualstudio.com/DefaultCollection/_apis/wit/attachments?fileName = Test& api-version = 1.0 "

这篇关于使用API​​为Visual Studio Team Services添加附件(是Visual Studio Online)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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