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

标签 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",

    }]

最佳答案

您错过了 Post_Data 中的“属性”部分,请尝试以下操作:

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

关于TFS 附件大小 0 KB 通过 REST 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46329101/

相关文章:

tfs - TFS 中是否有任何选项可以获取 "List of files reviewed by a reviewer"和 "History of reviewer for a file"?

tfs - 如何在 c# 项目的 TFS 上获取旧版本并对其进行编辑并检查旧版本中的编辑?

tfs - Nuget 安装程序任务在 tfs 2017 版本中失败

2012 年和 2017 年至 2017 年的 TFS 迁移

tfs - 如何在 Visual Studio Team Services 中为工作项添加自定义状态

tfs - 选择要放置 TFS 构建文件夹的内容

tfs - 工作项目 描述 高度不增加

tfs - 将工作目录移动到另一个驱动器并设置最大大小

json - 在 PowerShell 中组合 JSON 对象

azure - OAuth2 配置问题 : AADSTS50011: The reply url specified in the request does not match the reply urls configured for the application: