Rally C#:如何上传附件集合并与用户故事关联?

Rally C#:如何上传附件集合并与用户故事关联?,c#,api,rally,C#,Api,Rally,我参考了这个网站上的其他例子,但发现我的方法有一个很大的不同。(请耐心等待) 我试图遍历一个文件目录,将每个文件作为附件上传,并与用户故事关联。 我现在只能为一个用户故事附加1个文件。 我看到每个附件都必须被编码成一个base64字符串,并且它必须有一个以字节为单位的大小 以下是我目前的代码: public void createUsWithAttachmentList(string workspace, string project, string userStoryName, string

我参考了这个网站上的其他例子,但发现我的方法有一个很大的不同。(请耐心等待)

我试图遍历一个文件目录,将每个文件作为附件上传,并与用户故事关联。 我现在只能为一个用户故事附加1个文件。 我看到每个附件都必须被编码成一个base64字符串,并且它必须有一个以字节为单位的大小

以下是我目前的代码:

 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);
        }
    }
注意:我计划扩展此方法以支持多种文件类型,我想我需要获取目录中每个文件的文件类型并相应地继续。
关于如何写这篇文章,你有什么想法吗?

你已经实现了所有的部分-我们只需要稍微移动一下。在开始时创建一次故事,然后每次通过循环为每个文件创建一个新的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);
    }
}

是否可以将压缩目录作为附件推送到Rally中。如果是这样的话,你能提供一些建议吗。我开始认为它必须编码为int base 64 string。zip文件是一种有效的附件类型。您是正确的-内容需要进行base4编码。