且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用核心服务将外部文件导入 SDL Tridion 2011?

更新时间:2022-10-15 16:54:45

在此处阅读 Ryan 的优秀文章 http://blog.building-blocks.com/uploading-images-using-the-core-service-in-sdl-tridion-2011>

所有二进制文件都以相同的方式处理 - 因此他的图像技术对文档同样有效,只需确保使用具有适当 mime 类型的架构即可.

I want to push PDF, Word and Excel files into SDL Tridion 2011 by using core service.

I tried below code but get this error:

Invalid value for property 'BinaryContent'. Unable to open uploaded file:

using (ChannelFactory<ISessionAwareCoreService> factory =
    new ChannelFactory<ISessionAwareCoreService>("wsHttp_2011"))
{
  ISessionAwareCoreService client = factory.CreateChannel();
  ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(
                                       ItemType.Component, "tcm:19-483-2");
  multimediaComponent.Title = "MultimediaFile";

  multimediaComponent.ComponentType = ComponentType.Multimedia;
  multimediaComponent.Schema.IdRef = "tcm:19-2327-8";

  using (StreamUploadClient streamClient = new StreamUploadClient())
  {
    FileStream objfilestream = new FileStream(@"\My Documents\My Poc\images.jpg",
                                              FileMode.Open, FileAccess.Read);
    string tempLocation = streamClient.UploadBinaryContent("images.jpg",
                                                           objfilestream);
  }
  BinaryContentData binaryContent = new BinaryContentData();
  binaryContent.UploadFromFile = @"C:\Documents and Settings\My Poc\images.jpg";
  binaryContent.Filename = "images.jpg";
  binaryContent.MultimediaType = new LinkToMultimediaTypeData()
  {
    IdRef ="tcm:0-2-65544"
  };
  multimediaComponent.BinaryContent = binaryContent;

  IdentifiableObjectData savedComponent = client.Save(multimediaComponent,
                                                      new ReadOptions());

  client.CheckIn(savedComponent.Id, null);
  Response.Write(savedComponent.Id);
}    

Have a read of Ryan's excellent article here http://blog.building-blocks.com/uploading-images-using-the-core-service-in-sdl-tridion-2011

All binary files are handled the same way - so his technique for images will be equally as valid for documents, just make sure you use a Schema with the appropriate mime types.