c# - VersionOne API 客户端的附件

标签 c# sdk attachment versionone

有人可以展示一些示例代码,展示如何使用 API 客户端将外部文件附加到版本一请求吗?假设我们已经有了文件名和票证 ID。我发现这是通过 ObjectModel 完成的,但没有任何我可以通过 API 客户端理解的代码。

最佳答案

此示例将 rtf 文件附加到项目(范围)。您必须将范围更改为您选择的 Assets (可能是故事或缺陷)。这里我有 OID,而不是 ID。如果您有 ID.Number(例如 B-1234),您可以查询。

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VersionOne.SDK.APIClient; 

namespace SampleAttachment
{

    class Program
    {

        private static readonly string ApplicationURL = "https://myversionone/";

        private static readonly string _username = "username";

        private static readonly string _password = "passwerd";

        private static readonly bool _integrated = false;



        static void Main(string[] args)
        {

            //string file = args[0];
            string file = @"C:\Users\MIRVIN\Desktop\Training Wheels\SampleAttachment\bin\Debug\testfile.rtf";

            if (File.Exists(file))
            {

                Console.WriteLine("Uploading {0}", file);
                string mimeType = MimeType.Resolve(file);

                IMetaModel metaModel = new MetaModel(new V1APIConnector(ApplicationURL + "meta.v1/"));
                IServices services = new Services(metaModel, new V1APIConnector(ApplicationURL + "rest-1.v1/", _username, _password, _integrated));
                IAttachments attachments = new Attachments(new V1APIConnector(ApplicationURL + "attachment.img/", _username, _password, _integrated));


                //cleanjeans scope
                Oid attachmentContext = Oid.FromToken("Scope:5067", metaModel);



                IAssetType attachmentType = metaModel.GetAssetType("Attachment");

                IAttributeDefinition attachmentNameDef = attachmentType.GetAttributeDefinition("Name");

                IAttributeDefinition attachmentContentDef = attachmentType.GetAttributeDefinition("Content");

                IAttributeDefinition attachmentContentTypeDef = attachmentType.GetAttributeDefinition("ContentType");

                IAttributeDefinition attachmentFileNameDef = attachmentType.GetAttributeDefinition("Filename");



                Asset newAttachment = services.New(attachmentType, attachmentContext);

                newAttachment.SetAttributeValue(attachmentNameDef, "New Attachment");

                newAttachment.SetAttributeValue(attachmentContentDef, string.Empty);

                newAttachment.SetAttributeValue(attachmentContentTypeDef, mimeType);

                newAttachment.SetAttributeValue(attachmentFileNameDef, file);

                services.Save(newAttachment);

                //Setup and attach the payload

                string attachmentKey = newAttachment.Oid.Key.ToString();
                int buffersize = 4096;

                using (FileStream input = new FileStream(file, FileMode.Open, FileAccess.Read))
                {

                    using (Stream output = attachments.GetWriteStream(attachmentKey))
                    {

                        byte[] buffer = new byte[buffersize];

                        for (; ; )
                        {

                            int read = input.Read(buffer, 0, buffersize);

                            if (read == 0)

                                break;

                            output.Write(buffer, 0, read);

                        }

                    }

                }

                attachments.SetWriteStream(attachmentKey, mimeType);



                Console.WriteLine("{0} uploaded", file);

            }

            else

                Console.WriteLine("{0} does not exist", file);

        }

    }

        }

关于c# - VersionOne API 客户端的附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691205/

相关文章:

C# 连接两个数组的字符串

c# - 如何在reportviewer控件中传递报表参数

android - 如何调试android SDK类?

android - 如何将 SDK 版本从 26 更改为 19

bash - 使用 uuencode 和 mailx 发送电子邮件附件

android - 如何通过邮件从数据库发送图像

c# - ExtractToFile 抛出拒绝访问错误?

c# - 用于处理 Sitecore 中的虚拟项目的嵌套通配符

ios - 无法在 iPad 中使用应用程序内的 facebook 按钮注销

java - 如何通过java代码将任何文件作为附件附加到JIRA问题?