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.
The process for uploading binaries into Tridion using the Core Service is:
StreamUploadClient
. This returns you the path of the file on the Tridion server.BinaryContentData
that points to the file on the Tridion server (so with the path you got back from step 1)ComponentData
that refers to the the BinaryContentData
from step 2ComponentData
You are setting the local path for your file in step 2.
binaryContent.UploadFromFile = @"C:\Documents and Settings\My Poc\images.jpg";
But Tridion will never be able to find that file there. You instead should set the path that you got back from UploadBinaryContent
:
string tempLocation;
using (StreamUploadClient streamClient = new StreamUploadClient())
{
FileStream objfilestream = new FileStream(@"\My Documents\My Poc\images.jpg",
FileMode.Open, FileAccess.Read);
tempLocation = streamClient.UploadBinaryContent("images.jpg", objfilestream);
}
BinaryContentData binaryContent = new BinaryContentData();
binaryContent.UploadFromFile = tempLocation;
Note that the Ryan's original code does exactly that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With