有人能够展示一些示例代码,该代码展示了如何使用API客户端将外部文件附加到版本1请求?假设我们已经有文件名和票证号。我发现这是通过ObjectModel完成的,但不是我可以通过API客户端理解的任何代码。使用VersionOne API客户端的附件
回答
本示例将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);
}
}
}
我必须将V1APIConnector更改为VersionOneAPIConnector,并且对于凭据只需执行'NetworkCredential cred = new NetworkCredential();'然后将它们传递给'服务'和'附件',它完美地工作。 –
嘿,我有一个场景,我从一个交易所的电子邮件服务加载附件,并得到一个字节数组的内容,但我怎么写该字节数组到V1中的附件?谢谢。 – Thunder
@thunder你应该能够使用上面的大部分代码。遵循相同的步骤,首先创建一个容器资产,然后将内容流入其中。它说int buffersize = 4096?这应该是你的字节数组的大小。行byte [] buffer = new byte {buffer size]是创建要发送到VersionOne的有效内容的位置。确保你设置了mimeType以匹配正确的文件类型。 –
- 1. VersionOne API客户端不识别资产类型?
- 2. 使用Java客户端API
- 3. 附件在邮件客户端的ipad
- 4. 如何使用Gmail客户端API带有大附件的邮件在Android
- 5. 从RESTful API消费Spring Android客户端的图像附件
- 6. 使用Axis2附加客户端证书?
- 7. 使用RavenDB客户端API发送客户端证书
- 8. Sensenet:使用Sensenet客户端API初始化客户端内容
- 9. 将附件添加到客户端的默认电子邮件客户端
- 10. 编写使用库的客户端API
- 11. 使用Windows API的NTP客户端
- 12. 打开Outlook客户端附件
- 13. ASP.NET API客户端
- 14. C#SharePoint客户端列表附件
- 15. RESTful API客户端
- 16. 在Web客户端编辑附件
- 17. GWT客户端端rest API
- 18. RESTEasy客户端API
- 19. CakePHP 3 - 使用Google API客户端
- 20. 在API客户端库中使用login_hint
- 21. Java Rest客户端API使用未来
- 22. 如何在客户端使用rest api?
- 23. 如何使用Google API客户端库?
- 24. 通过java客户端API使用marklogic?
- 25. 如何从客户端使用Mandrillapp API?
- 26. 使用谷歌API PHP客户端库
- 27. 关于使用谷歌客户端api
- 28. 使用http客户端API构建URI
- 29. NodeJS api - 客户端的“net”
- 30. 客户端的Instagram API
请标记MarkoPolo的答案为已接受,它的工作原理。 –