问题
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);
}
回答1:
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.
回答2:
The process for uploading binaries into Tridion using the Core Service is:
- Upload the binary data to the Tridion server using a
StreamUploadClient
. This returns you the path of the file on the Tridion server. - Create a
BinaryContentData
that points to the file on the Tridion server (so with the path you got back from step 1) - Create a
ComponentData
that refers to the theBinaryContentData
from step 2 - Save the
ComponentData
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.
来源:https://stackoverflow.com/questions/10447494/how-can-i-import-external-files-into-sdl-tridion-2011-using-core-service