【问题标题】:Attachments with the VersionOne API ClientVersionOne API 客户端的附件
【发布时间】:2013-10-30 18:39:01
【问题描述】:

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

【问题讨论】:

  • 请将 MarkoPolo 的答案标记为已接受,它有效。

标签: c# sdk attachment versionone


【解决方案1】:

此示例将 rtf 文件附加到项目(范围)。您必须将范围更改为您选择的资产(可能的故事或缺陷)。这里我有 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);

        }

    }

        }

【讨论】:

  • 我必须将 V1APIConnector 更改为 VersionOneAPIConnector 并且对于凭据只需执行 'NetworkCredential cred = new NetworkCredential();'然后将它们传递给“服务”和“附件”,它可以完美运行。
  • 嘿,我有一个场景,我从交换电子邮件服务加载附件并获取字节数组中的内容,但是如何将该字节数组写入 V1 中的附件?谢谢。
  • @thunder 你应该可以使用上面的大部分代码。遵循相同的步骤,首先创建一个容器资产,然后将内容流式传输到其中。它在哪里说int buffersize = 4096?那应该是您的字节数组的大小。行 byte[] buffer = new byte{buffer size] 是您创建要发送到 VersionOne 的有效负载的位置。确保设置 mimeType 以匹配正确的文件类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 2021-08-30
  • 1970-01-01
  • 2012-08-09
  • 2019-09-02
  • 1970-01-01
  • 2011-08-27
相关资源
最近更新 更多