6

我有一个要求,我需要下载多媒体组件的二进制文件,但是当我访问BinaryContentData类公开的属性时,没有下载图像文件的属性。虽然对于上传文件,核心服务有一个属性,即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;

请建议。

4

1 回答 1

5

您可以使用Tridion.ContentManager.CoreService.Client.dllstreamDownloadClient.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();
} 
于 2012-10-10T10:06:24.863 回答