当前位置: 首页 > 知识库问答 >
问题:

使用C#和从rally下载附件。网

云昊阳
2023-03-14

我写的剧本的最终目标是从rally下载所有附件。我能够成功地连接到服务器,请求工作区中的所有附件,最后,通过迭代检索每个附件的内容。这将生成“AttachmentContent”的动态对象。对于任何API来说都是新的#和。NET,我现在被卡住了。我无法找到一种方法来访问此对象的内容并将其下载到我的计算机上的文件中。我在下面评论的这句话是我当前遇到错误并被卡住的地方。任何帮助都将不胜感激。提前谢谢!

这里是我的主要方法:

static void Main(string[] args)
    {
        RallyRestApi restApi = new RallyRestApi("user@company.com", "password", "https://rally1.rallydev.com", "1.43");
        Request request = new Request("attachment");
        request.Workspace = "/workspace/186282018";
        request.Fetch = new List<string>() { "Name", "Artifact", "Content", "ContentType" };
        request.Query = new Query("");
        QueryResult queryResult = restApi.Query(request);
        int count = 0;
        foreach (var result in queryResult.Results)
        {     
            DynamicJsonObject content = result["Content"];
            //var binContent = content["Content"];
            count++;
        }
        Console.WriteLine(count);
        Console.ReadLine();
    }

共有3个答案

仲孙飞文
2023-03-14

下面是来自user984832的同一个示例,但已修改为使用v2。WS-API的0,以及Rally REST toolkit的2.0.1 dll。网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Rally.RestApi;
using Rally.RestApi.Response;

namespace DownloadAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
            RallyRestApi restApi;

            String userName = "user@co.com";
            String userPassword = "secret";

            String rallyURL = "https://rally1.rallydev.com";
            String wsapiVersion = "v2.0";

            restApi = new RallyRestApi(
                userName,
                userPassword,
                rallyURL,
                wsapiVersion
            );

            String workspaceRef = "/workspace/12352608129";
            String projectRef = "/project/12352608219";
            bool projectScopingUp = false;
            bool projectScopingDown = true;

            Request storyRequest = new Request("hierarchicalrequirement");
            storyRequest.Workspace = workspaceRef;
            storyRequest.Project = projectRef;
            storyRequest.ProjectScopeDown = projectScopingDown;
            storyRequest.ProjectScopeUp = projectScopingUp;

            storyRequest.Fetch = new List<string>()
                {
                    "Name",
                    "FormattedID",
                    "Attachments"
                };

            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US20");
            QueryResult queryResult = restApi.Query(storyRequest);
            DynamicJsonObject story = queryResult.Results.First();

            // Grab the Attachments collection
            Request attachmentsRequest = new Request(story["Attachments"]);
            QueryResult attachmentsResult = restApi.Query(attachmentsRequest);

            //Download the first attachment

            var myAttachmentFromStory = attachmentsResult.Results.First();
            String myAttachmentRef = myAttachmentFromStory["_ref"];
            Console.WriteLine("Found Attachment: " + myAttachmentRef);

            // Fetch fields for the Attachment
            string[] attachmentFetch = { "ObjectID", "Name", "Content", "ContentType", "Size" };

            // Now query for the attachment
            DynamicJsonObject attachmentObject = restApi.GetByReference(myAttachmentRef, "true");

            // Grab the AttachmentContent
            DynamicJsonObject attachmentContentFromAttachment = attachmentObject["Content"];
            String attachmentContentRef = attachmentContentFromAttachment["_ref"];

            // Lastly pull the content
            // Fetch fields for the Attachment
            string[] attachmentContentFetch = { "ObjectID", "Content" };

            // Now query for the attachment
            Console.WriteLine("Querying for Content...");
            DynamicJsonObject attachmentContentObject = restApi.GetByReference(attachmentContentRef, "true");
            Console.WriteLine("AttachmentContent: " + attachmentObject["_ref"]);

            String base64EncodedContent = attachmentContentObject["Content"];

            // File information
            String attachmentSavePath = "C:\\Users\\nmusaelian\\NewFolder";
            String attachmentFileName = attachmentObject["Name"];
            String fullAttachmentFile = attachmentSavePath + attachmentFileName;

            // Determine attachment Content mime-type
            String attachmentContentType = attachmentObject["ContentType"];

            // Specify Image format
            System.Drawing.Imaging.ImageFormat attachmentImageFormat;

            try
            {
                attachmentImageFormat = getImageFormat(attachmentContentType);
            }
            catch (System.ArgumentException e)
            {
                Console.WriteLine("Invalid attachment file format:" + e.StackTrace);
            }

            try
            {

                // Convert base64 content to Image
                Console.WriteLine("Converting base64 AttachmentContent String to Image.");

                // Convert Base64 string to bytes
                byte[] bytes = Convert.FromBase64String(base64EncodedContent);

                Image myAttachmentImage;
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    myAttachmentImage = Image.FromStream(ms);
                    // Save the image
                    Console.WriteLine("Saving Image: " + fullAttachmentFile);
                    myAttachmentImage.Save(fullAttachmentFile, System.Drawing.Imaging.ImageFormat.Jpeg);

                    Console.WriteLine("Finished Saving Attachment: " + fullAttachmentFile);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Unhandled exception occurred: " + e.StackTrace);
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }

        // Returns an ImageFormat type based on Rally contentType / mime-type
        public static System.Drawing.Imaging.ImageFormat getImageFormat(String contentType)
        {
            // Save Image format
            System.Drawing.Imaging.ImageFormat attachmentImageFormat;

            switch (contentType)
            {
                case "image/png":
                    attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Png;
                    break;
                case "image/jpeg":
                    attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                case "image/tiff":
                    attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Tiff;
                    break;
                default:
                    Console.WriteLine("Invalid image file format.");
                    throw new System.ArgumentException("Invalid attachment file format.");
            };

            return attachmentImageFormat;
        }
    }
}
费凯康
2023-03-14

下面是一个简单的示例,说明下载附件内容的过程。它的类型处理仅限于几种常见的图像类型,但它说明了这一点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Rally.RestApi;
using Rally.RestApi.Response;

namespace RestExample_DownloadAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
             //Initialize the REST API
            RallyRestApi restApi;

            // Rally parameters
            String userName = "user@company.com";
            String userPassword = "topsecret";
            String rallyURL = "https://rally1.rallydev.com";
            String wsapiVersion = "1.43";

            restApi = new RallyRestApi(
                userName,
                userPassword,
                rallyURL,
                wsapiVersion
            );

            //Set our Workspace and Project scopings
            String workspaceRef = "/workspace/12345678910";
            String projectRef = "/project/12345678911";
            bool projectScopingUp = false;
            bool projectScopingDown = true;

            // Find User Story that we want to pull attachment from

            // Tee up Story Request
            Request storyRequest = new Request("hierarchicalrequirement");
            storyRequest.Workspace = workspaceRef;
            storyRequest.Project = projectRef;
            storyRequest.ProjectScopeDown = projectScopingDown;
            storyRequest.ProjectScopeUp = projectScopingUp;

            // Fields to Fetch
            storyRequest.Fetch = new List<string>()
                {
                    "Name",
                    "FormattedID",
                    "Attachments"
                };

            // Add a query
            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US43");

            // Query Rally for the Story
            QueryResult queryResult = restApi.Query(storyRequest);

            // Pull reference off of Story fetch
            DynamicJsonObject storyObject = queryResult.Results.First();
            String storyReference = storyObject["_ref"];
            Console.WriteLine("Looking for attachments off of Story: " + storyReference);

            // Grab the Attachments collection
            var storyAttachments = storyObject["Attachments"];
            // Let's download the first attachment for starters
            var myAttachmentFromStory = storyAttachments[0];
            // Pull the ref
            String myAttachmentRef = myAttachmentFromStory["_ref"];
            Console.WriteLine("Found Attachment: " + myAttachmentRef);

            // Fetch fields for the Attachment
            string[] attachmentFetch = { "ObjectID", "Name", "Content", "ContentType", "Size"};

            // Now query for the attachment
            DynamicJsonObject attachmentObject = restApi.GetByReference(myAttachmentRef, "true");

            // Grab the AttachmentContent
            DynamicJsonObject attachmentContentFromAttachment = attachmentObject["Content"];
            String attachmentContentRef = attachmentContentFromAttachment["_ref"];

            // Lastly pull the content
            // Fetch fields for the Attachment
            string[] attachmentContentFetch = { "ObjectID", "Content" };

            // Now query for the attachment
            Console.WriteLine("Querying for Content...");
            DynamicJsonObject attachmentContentObject = restApi.GetByReference(attachmentContentRef, "true");
            Console.WriteLine("AttachmentContent: " + attachmentObject["_ref"]);

            String base64EncodedContent = attachmentContentObject["Content"];

            // File information
            String attachmentSavePath = "C:\\Users\\username\\";
            String attachmentFileName = attachmentObject["Name"];
            String fullAttachmentFile = attachmentSavePath + attachmentFileName; 

            // Determine attachment Content mime-type
            String attachmentContentType = attachmentObject["ContentType"];

            // Specify Image format
            System.Drawing.Imaging.ImageFormat attachmentImageFormat;

            try
            {
                attachmentImageFormat = getImageFormat(attachmentContentType);
            }
            catch (System.ArgumentException e)
            {
                Console.WriteLine("Invalid attachment file format:" + e.StackTrace);
                Console.WriteLine("Don't know how to handle: " + attachmentContentType);
                return;
            }

            try {

                // Convert base64 content to Image
                Console.WriteLine("Converting base64 AttachmentContent String to Image.");

                // Convert Base64 string to bytes
                byte[] bytes = Convert.FromBase64String(base64EncodedContent);

                Image myAttachmentImage;
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    myAttachmentImage = Image.FromStream(ms);
                    // Save the image
                    Console.WriteLine("Saving Image: " + fullAttachmentFile);
                    myAttachmentImage.Save(fullAttachmentFile, System.Drawing.Imaging.ImageFormat.Jpeg);

                    Console.WriteLine("Finished Saving Attachment: " + fullAttachmentFile);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Unhandled exception occurred: " + e.StackTrace);
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }

        // Returns an ImageFormat type based on Rally contentType / mime-type
        public static System.Drawing.Imaging.ImageFormat getImageFormat(String contentType)
        {
            // Save Image format
            System.Drawing.Imaging.ImageFormat attachmentImageFormat;

            switch (contentType)
            {
                case "image/png":
                    attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Png;
                    break;
                case "image/jpeg":
                    attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                case "image/tiff":
                    attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Tiff;
                    break;
                default:
                    Console.WriteLine("Invalid image file format.");
                    throw new System.ArgumentException("Invalid attachment file format.");
            };

            return attachmentImageFormat;
        }
    }
}
华恩
2023-03-14

下面是一个简单地将内容字节写入文件并跳过mime类型检查的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Rally.RestApi;
using Rally.RestApi.Response;

namespace RestExample_DownloadAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
             //Initialize the REST API
            RallyRestApi restApi;

            // Rally parameters
            String userName = "user@company.com";
            String userPassword = "topsecret";
            String rallyURL = "https://rally1.rallydev.com";
            String wsapiVersion = "1.43";

            restApi = new RallyRestApi(
                userName,
                userPassword,
                rallyURL,
                wsapiVersion
            );


            //Set our Workspace and Project scopings
            String workspaceRef = "/workspace/12345678910";
            String projectRef = "/project/12345678911";
            bool projectScopingUp = false;
            bool projectScopingDown = true;

            // Find User Story that we want to pull attachment from

            // Tee up Story Request
            Request storyRequest = new Request("hierarchicalrequirement");
            storyRequest.Workspace = workspaceRef;
            storyRequest.Project = projectRef;
            storyRequest.ProjectScopeDown = projectScopingDown;
            storyRequest.ProjectScopeUp = projectScopingUp;

            // Fields to Fetch
            storyRequest.Fetch = new List<string>()
                {
                    "Name",
                    "FormattedID",
                    "Attachments"
                };

            // Add a query
            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US163");

            // Query Rally for the Story
            QueryResult queryResult = restApi.Query(storyRequest);

            // Pull reference off of Story fetch
            DynamicJsonObject storyObject = queryResult.Results.First();
            String storyReference = storyObject["_ref"];
            Console.WriteLine("Looking for attachments off of Story: " + storyReference);

            // Grab the Attachments collection
            var storyAttachments = storyObject["Attachments"];
            // Let's download the first attachment for starters
            var myAttachmentFromStory = storyAttachments[0];
            // Pull the ref
            String myAttachmentRef = myAttachmentFromStory["_ref"];
            Console.WriteLine("Found Attachment: " + myAttachmentRef);

            // Fetch fields for the Attachment
            string[] attachmentFetch = { "ObjectID", "Name", "Content", "ContentType", "Size"};

            // Now query for the attachment
            DynamicJsonObject attachmentObject = restApi.GetByReference(myAttachmentRef, "true");

            // Grab the AttachmentContent
            DynamicJsonObject attachmentContentFromAttachment = attachmentObject["Content"];
            String attachmentContentRef = attachmentContentFromAttachment["_ref"];

            // Lastly pull the content
            // Fetch fields for the Attachment
            string[] attachmentContentFetch = { "ObjectID", "Content" };

            // Now query for the attachment
            Console.WriteLine("Querying for Content...");
            DynamicJsonObject attachmentContentObject = restApi.GetByReference(attachmentContentRef, "true");
            Console.WriteLine("AttachmentContent: " + attachmentObject["_ref"]);

            String base64EncodedContent = attachmentContentObject["Content"];

            // File information
            String attachmentSavePath = "C:\\Users\\username\\";
            String attachmentFileName = attachmentObject["Name"];
            String fullAttachmentFile = attachmentSavePath + attachmentFileName; 

            // Determine attachment Content mime-type
            String attachmentContentType = attachmentObject["ContentType"];

            try {

                // Output base64 content to File
                Console.WriteLine("Saving base64 AttachmentContent String to File.");

                File.WriteAllBytes(@fullAttachmentFile, Convert.FromBase64String(base64EncodedContent));
            }
            catch (Exception e)
            {
                Console.WriteLine("Unhandled exception occurred while writing file: " + e.StackTrace);
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
    }
}
 类似资料:
  • 如何从内容类型为“多部分/备选”的邮件中下载附件;

  • 问题内容: 我查看了参考文档,Spring似乎对发送邮件有很好的支持。但是,我需要登录到邮件帐户,阅读邮件并下载所有附件。Spring mail API是否支持下载邮件附件? 我知道您可以使用Java Mail API来执行此操作,但是在过去,我发现使用它非常冗长且令人不愉快。 编辑 :我收到了一些指向教程的答复,这些教程描述了如何发送带有附件的邮件,但是我要问的是如何从 收到的 邮件中 读取 附

  • 我正在一个apache camel项目中下载收件箱中的任何附件,以实现我遵循http://camel.apache.org/mail.html中使用附件的邮件示例中的信息。除了附件的文件名带有特殊的字符或空格外,其他操作都很正常。例如,如果附件的文件名为“voucher.pdf”,则程序将运行并下载该文件,但如果附件的文件名为“Pase de abordar en Línea(1).pdf”,则会

  • 我们正在移植我们的产品。NET将代码从SOAP集合到其他程序。NET API。到目前为止,REST API似乎更快、更易于使用,因为在Rally工作区中,每次工作产品自定义字段发生更改时,都没有WSDL可以中断。 不过,当我们试图复制上传附件的能力时,我遇到了一个问题。我们遵循的程序与本文概述的程序非常相似: Rally SOAP API-如何向分层需求添加附件 将图像读入系统。绘画形象我们使用I

  • 我想从一个邮箱中搜索名字中包含某些关键字的所有附件,我正在使用C#EWS托管API(2.2版)来完成这一操作。我可以使用Item.hasAttachment:true属性访问带有附件的项,并且代码按预期工作。但办理时间很长。 我需要知道是否有一个更好更快的方法来访问一个邮箱/文件夹中的附件使用EWS。不是检查每一个邮件项目,有没有一种方法应用过滤器的附件在文件夹级别? 下面是用于按name关键字提

  • 我正在使用JSCH从SFTP服务器下载文件。我使用单会话,多通道下载文件从不同文件夹位于SFTP。对于这个下载过程,我有一组排定的作业。每项工作将: 每次打开一个新通道()。通道名称:SFTP 使用方法获取要下载的文件总数的大小 如果size(Vector)大于零,则使用下载所有文件 最后关闭打开的通道。 在上面的过程中,大多数时候我得到的文件,找不到或没有这样的文件异常,并没有下载一些文件。 谁