我有一个要求,我需要下载多媒体组件的二进制文件,但是当我访问BinaryContentData
类的公开属性时,没有属性可以下载图像文件。虽然对于上传文件,Core Service有一个属性UploadFromFile
。
有没有办法将二进制文件下载到临时位置。以下是我正在使用的代码:
core_service.ServiceReference1.SessionAwareCoreService2010Client client = new SessionAwareCoreService2010Client();
client.ClientCredentials.Windows.ClientCredential.UserName = "myUserName";
client.ClientCredentials.Windows.ClientCredential.Password = "myPassword"; client.Open();
ComponentData component = (ComponentData)client.TryCheckOut(
multimediaComponentURI, new ReadOptions());
BinaryContentData binaryData = component.BinaryContent;
请建议。
答案 0 :(得分:5)
您可以使用 Tridion.ContentManager.CoreService.Client.dll 中的一个名为streamDownloadClient.DownloadBinaryContent
的辅助函数。
我创建了以下通常用于此目的的函数:
private static void CreateBinaryFromMultimediaComponent(string tcm)
{
Tridion.ContentManager.CoreService.Client.StreamDownloadClient streamDownloadClient = new StreamDownloadClient();
SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2011");
ComponentData multimediaComponent = client.Read(tcm, new ReadOptions()) as ComponentData;
// Generate you own file name, and file location
string file = "D:\\MyTempLocation\\" + Path.GetFilename(multimediaComponent.BinaryContent.Filename);;
// Write out the existing file from Tridion
FileStream fs = File.Create(file);
byte[] binaryContent = null;
if (multimediaComponent.BinaryContent.FileSize != -1)
{
Stream tempStream = streamDownloadClient.DownloadBinaryContent(tcm);
var memoryStream = new MemoryStream();
tempStream.CopyTo(memoryStream);
binaryContent = memoryStream.ToArray();
}
fs.Write(binaryContent, 0, binaryContent.Length);
fs.Close();
}