有人能夠展示一些示例代碼,該代碼展示瞭如何使用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的答案爲已接受,它的工作原理。 –