2017-01-09 34 views
1

我已審閱本網站上的其他例子,卻發現我的方法的主要區別。 (請耐心等待)拉力賽C#:如何上傳附件的收集和準與用戶故事?

我試圖遍歷文件的目錄和上傳每個文件作爲附件和準用戶故事。 我只能夠連接1個文件的用戶故事的現在。 我看到每一個附件具有被編碼到一個基座64的字符串並且它必須在字節具有一個尺寸。

這是到目前爲止我的代碼:

public void createUsWithAttachmentList(string workspace, string project, string userStoryName, string userStoryDescription) 
    { 

     //authentication 
     this.EnsureRallyIsAuthenticated(); 

     //DynamicJSONObject for AttachmentContent 
     DynamicJsonObject myAttachmentContent = new DynamicJsonObject(); 

     //Length calculated from Base64String converted back 
     int imageNumberBytes = 0; 

     //Userstory setup 
     DynamicJsonObject toCreate = new DynamicJsonObject(); 
     toCreate["Workspace"] = workspace; 
     toCreate["Project"] = project; 
     toCreate["Name"] = userStoryName; 
     toCreate["Description"] = userStoryDescription; 

     //Trying to get a list of all the file paths within a given directory, this directory would contain .png files that need to be associated to a user story. 
     string[] attachmentPath = Directory.GetFiles("C:\\Users\\user\\Desktop\\RallyAttachments"); 

這foreach循環是混亂的。我試圖遍歷目錄中的每個文件,以便將其轉換爲base64字符串,並且同時獲取每個文件的字節數作爲int。

 foreach (string fileName in attachmentPath) 
     { 
      Image myImage = Image.FromFile(fileName); 
      string imageBase64String = imageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Png); 
      imageNumberBytes = Convert.FromBase64String(imageBase64String).Length; 

      //I am stuck here to be exact because there are multiple imageBase64Strings due to the collection of files located inside the directory. AND the below line is wrong because I have a list of imageBase64Strings that were generated from iterating through the string[] attachmentPath. 
      myAttachmentContent[RallyField.content] = imageBase64String; 
     } 

     try 
     { 
      //create user story 
      CreateResult createUserStory = _api.Create(RallyField.attachmentContent, myAttachmentContent); 
      //create attachment 
      CreateResult myAttachmentContentCreateResult = _api.Create(RallyField.attachmentContent, myAttachmentContent); 
      String myAttachmentContentRef = myAttachmentContentCreateResult.Reference; 

      //DynamicJSONObject for Attachment Container 
      //I assume I would need a separate container for each file in my directory containing the attachments. 
      DynamicJsonObject myAttachment = new DynamicJsonObject(); 
      myAttachment["Artifact"] = createUserStory.Reference; 
      myAttachment["Content"] = myAttachmentContentRef; 
      myAttachment["Name"] = "AttachmentFromREST.png"; 
      myAttachment["Description"] = "Email Attachment"; 
      myAttachment["ContentType"] = "image/png"; 
      myAttachment["Size"] = imageNumberBytes; 

      //create & associate the attachment 
      CreateResult myAttachmentCreateResult = _api.Create(RallyField.attachment, myAttachment); 
      Console.WriteLine("Created User Story: " + createUserStory.Reference); 
     } 
     catch (WebException e) 
     { 
      Console.WriteLine(e.Message); 
     } 
    } 

注:我打算擴大這種方法支持多種文件類型,我的事情我需要得到目錄中的每個文件的文件類型並進行相應處理。 關於如何寫這個的任何想法?

回答

1

你已經完成了所有的部分 - 我們只需要將它移動一點。在開始創建的故事一次,然後通過循環每次做一個新AttachmentContent併爲每個文件的附件。

public void createUsWithAttachmentList(string workspace, string project, string userStoryName, string userStoryDescription) 
{ 

    //authentication 
    this.EnsureRallyIsAuthenticated(); 

    //Userstory setup 
    DynamicJsonObject toCreate = new DynamicJsonObject(); 
    toCreate["Workspace"] = workspace; 
    toCreate["Project"] = project; 
    toCreate["Name"] = userStoryName; 
    toCreate["Description"] = userStoryDescription; 

    //Create the story first 
    try 
    { 
     //create user story 
     CreateResult createUserStory = _api.Create(RallyField.userStory, toCreate); 


     //now loop over each file 
     string[] attachmentPath = Directory.GetFiles("C:\\Users\\user\\Desktop\\RallyAttachments"); 

     foreach (string fileName in attachmentPath) 
     { 
      //DynamicJSONObject for AttachmentContent 
      DynamicJsonObject myAttachmentContent = new DynamicJsonObject(); 
      Image myImage = Image.FromFile(fileName); 
      string imageBase64String = imageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Png); 
      int imageNumberBytes = Convert.FromBase64String(imageBase64String).Length; 
      myAttachmentContent[RallyField.content] = imageBase64String; 

      //create the AttachmentConent 
      CreateResult myAttachmentContentCreateResult = _api.Create(RallyField.attachmentContent, myAttachmentContent); 
      String myAttachmentContentRef = myAttachmentContentCreateResult.Reference; 

      //create an Attachment to associate to story 
      DynamicJsonObject myAttachment = new DynamicJsonObject(); 
      myAttachment["Artifact"] = createUserStory.Reference; 
      myAttachment["Content"] = myAttachmentContentRef; 
      myAttachment["Name"] = "AttachmentFromREST.png"; 
      myAttachment["Description"] = "Email Attachment"; 
      myAttachment["ContentType"] = "image/png"; 
      myAttachment["Size"] = imageNumberBytes; 

      //create & associate the attachment 
      CreateResult myAttachmentCreateResult = _api.Create(RallyField.attachment, myAttachment); 
     }  
    } 
    catch (WebException e) 
    { 
     Console.WriteLine(e.Message); 
    } 
} 
+0

是否可以將壓縮目錄作爲附件壓入拉力賽。如果可以,你可以提供任何指針。我開始認爲它必須被編碼爲int base 64字符串。 –

+0

zip文件是有效的附件類型。你是對的 - 內容需要以base4編碼。 –

相關問題