通过REST调用TFS附加大小0KB

时间:2017-09-20 18:14:45

标签: tfs azure-devops

每当我尝试通过REST调用将附件附加到TFS WorkItem时,附件大小为0KB。

首先,我使用以下代码在附件存储中上传附件。

https://{instance}/DefaultCollection/_apis/wit/attachments?api-version=1.0&filename="{fileName}"

我通过rest调用以字节数组发送数据。在此之后,我将该附件附加到工作项目。

附加附件成功,但附件大小为零KB 是否存在TFS问题或我做错了什么?

我使用C#语言进行编程,使用REST Sharp访问VSTS API

 Dim restClient = New RestClient("Server URL")

    restClient.Authenticator = New HttpBasicAuthenticator("UserId", "Password")

    Dim request = New RestRequest("API_Name", Method.POST)

    request.AlwaysMultipartFormData = False

    request.AddParameter(String.Format("{0}; charset=utf-8", contentType), File.ReadAllBytes(filePath), ParameterType.RequestBody)

    request.RequestFormat = DataFormat.Json

    Dim response As IRestResponse = restClient.Execute(request)

    Return response

我以字节发送文件数据。

使用WorkItem附加附件。

    Dim restClient = New RestClient(ACCESS_URL)
    restClient.Authenticator = New HttpBasicAuthenticator(USER_NAME, PASSWORD)
    Dim request = New 
    RestRequest("CollectionName}/_apis/wit/workitems/{WorkItem_ID}", Method.PATCH)

   request.AddParameter("application/json-patch+json; charset=utf-8", 
   "post_Data", ParameterType.RequestBody)

   request.RequestFormat = DataFormat.Json

   Dim response As IRestResponse = restClient.Execute(request)
   Return response

Post_Data是一个json字符串,它接受这种类型的数据

[{
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "AttachementURI",

    }]

2 个答案:

答案 0 :(得分:1)

{ file-contents }保留为空时,我可以重现此问题。

因此,请确保您已指定{ file-contents }

  

将文件附加到工作项upload the attachment到   附件商店,然后将其附加到工作项。有关详细信息,请参阅Add an attachment

上传附件:

POST https://{instance}/DefaultCollection/_apis/wit/attachments?api-version={version}&filename=Spec.txt
Content-Type: application/octet-stream
{ file-contents }

为特定工作项添加附件:

PATCH https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workitems/299?api-version=1.0
Content-Type: application/json-patch+json
[
  {
    "op": "test",
    "path": "/rev",
    "value": 3
  },
  {
    "op": "add",
    "path": "/fields/System.History",
    "value": "Adding the necessary spec"
  },
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/attachments/098a279a-60b9-40a8-868b-b7fd00c0a439?fileName=Spec.txt",
      "attributes": {
        "comment": "Spec for the work"
      }
    }
  }
]

请参阅下面的C#示例上传并添加工作项的附件:

enter image description here

答案 1 :(得分:0)

你错过了"属性"在Post_Data中的部分,请尝试以下内容:

[{
    "op": "add",
    "path": "/relations/-",
    "value": {
    "rel": "AttachedFile",
    "url": "AttachementURI",
    "attributes": {
       }
}]