Tridion核心服务如何下载多媒体组件的二进制文件

时间:2012-10-10 10:01:52

标签: tridion tridion-2011 core-services

我有一个要求,我需要下载多媒体组件的二进制文件,但是当我访问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;

请建议。

1 个答案:

答案 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();
}