2013-10-30 67 views
2

有人能夠展示一些示例代碼,該代碼展示瞭如何使用API​​客戶端將外部文件附加到版本1請求?假設我們已經有文件名和票證號。我發現這是通過ObjectModel完成的,但不是我可以通過API客戶端理解的任何代碼。使用VersionOne API客戶端的附件

+1

請標記MarkoPolo的答案爲已接受,它的工作原理。 –

回答

4

本示例將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); 

     } 

    } 

     } 
+0

我必須將V1APIConnector更改爲VersionOneAPIConnector,並且對於憑據只需執行'Ne​​tworkCredential cred = new NetworkCredential();'然後將它們傳遞給'服務'和'附件',它完美地工作。 –

+0

嘿,我有一個場景,我從一個交易所的電子郵件服務加載附件,並得到一個字節數組的內容,但我怎麼寫該字節數組到V1中的附件?謝謝。 – Thunder

+0

@thunder你應該能夠使用上面的大部分代碼。遵循相同的步驟,首先創建一個容器資產,然後將內容流入其中。它說int buffersize = 4096?這應該是你的字節數組的大小。行byte [] buffer = new byte {buffer size]是創建要發送到VersionOne的有效內容的位置。確保你設置了mimeType以匹配正確的文件類型。 –