我们正在将 .NET Rally 代码从 SOAP 移植到 REST .NET API。到目前为止一切顺利,REST API 似乎更快并且更易于使用,因为每次工作产品自定义字段在 Rally 工作区中更改时都不会中断 WSDL。
不过,当我们尝试复制上传附件的功能时,我遇到了一件事。我们正在遵循与本文中概述的非常相似的程序:
从而将图像读入 System.Drawing.Image。我们使用 ImageToByteArray 函数将图像转换为字节数组,然后将其分配给首先创建的 AttachmentContent。
然后,创建 Attachment,并将其连接到 AttachmentContent 和 HierarchicalRequirement。
所有的创作活动都很好。内容对象被创建得很好。然后创建名为“Image.png”的新附件并将其链接到故事。但是当我从 Rally 下载生成的附件时,Image.png 的字节数为零!我已经用不同的图像、JPEG、PNG 等进行了尝试,结果都相同。
下面是显示我们流程的代码摘录。我有什么明显的遗漏吗?提前致谢。
// .... Read content into a System.Drawing.Image called imageObject ....
// Convert Image to byte array
byte[] imageBytes = ImageToByteArray(imageObject, System.Drawing.Imaging.ImageFormat.Png);
var imageLength = imageBytes.Length;
// AttachmentContent
DynamicJsonObject attachmentContent = new DynamicJsonObject();
attachmentContent["Content"] = imageBytes ;
CreateResult cr = restApi.Create("AttachmentContent", myAttachmentContent);
String contentRef = cr.Reference;
Console.WriteLine("Created: " + contentRef);
// Set up attachment
DynamicJsonObject newAttachment = new DynamicJsonObject();
newAttachment["Artifact"] = story;
newAttachment["Content"] = attachmentContent;
newAttachment["Name"] = "Image.png";
newAttachment["ContentType"] = "image/png";
newAttachment["Size"] = imageLength;
newAttachment["User"] = user;
// Create the attachment in Rally
cr = restApi.Create("Attachment", newAttachment);
String attachRef = cr.Reference;
Console.WriteLine("Created: " + attachRef);
}
public static byte[] ImageToByteArray(Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, format);
// Convert Image to byte[]
byte[] imageBytes = ms.ToArray();
return imageBytes;
}
}