예제 #1
0
    //http://blog.building-blocks.com/uploading-images-using-the-core-service-in-sdl-tridion-2011
    public static void UploadImages(string location, string folderTcmId, CoreService2010Client client, log4net.ILog Log)
    {
        //create a reference to the directory of where the images are
        DirectoryInfo directory = new DirectoryInfo(location);
        //create global Tridion Read Options
        ReadOptions readOptions = new ReadOptions();

        //use Expanded so that Tridion exposes the TcmId of the newly created component
        readOptions.LoadFlags = LoadFlags.Expanded;
        try
        {
            //loop through the files
            foreach (FileInfo fileInfo in directory.GetFiles())
            {
                //only allow images
                if (IsAllowedFileType(fileInfo.Extension))
                {
                    try
                    {
                        //create a new multimedia component in the folder specified
                        ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(Tridion.ItemType.Component, folderTcmId);
                        multimediaComponent.Title         = fileInfo.Name.ToLower();
                        multimediaComponent.ComponentType = ComponentType.Multimedia;
                        multimediaComponent.Schema.IdRef  = ConfigurationManager.AppSettings["MultimediaSchemaId"];

                        //create a string to hold the temporary location of the image to use later
                        string tempLocation = "";

                        //use the StreamUpload2010Client to upload the image into Tridion
                        UploadResponse us = new UploadResponse();
                        using (Tridion.StreamUpload2010Client streamClient = new StreamUpload2010Client())
                        {
                            FileStream objfilestream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
                            tempLocation = streamClient.UploadBinaryContent(fileInfo.Name.ToLower(), objfilestream);
                        }

                        //creat a new binary component
                        BinaryContentData binaryContent = new BinaryContentData();
                        //set this temporary upload location to the source of this binary
                        binaryContent.UploadFromFile = tempLocation;
                        binaryContent.Filename       = fileInfo.Name.ToLower();

                        //get the multimedia type id
                        binaryContent.MultimediaType = new LinkToMultimediaTypeData()
                        {
                            IdRef = GetMultimediaTypeId(fileInfo.Extension)
                        };

                        multimediaComponent.BinaryContent = binaryContent;

                        //save the image into a new object
                        IdentifiableObjectData savedComponent = client.Save(multimediaComponent, readOptions);

                        //check in using the Id of the new object
                        client.CheckIn(savedComponent.Id, null);
                    }
                    catch (Exception ex)
                    {
                        Log.Debug("Error creating image " + fileInfo.Name, ex);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error("Error processing images", ex);
        }
        finally
        {
            //clean up temp objects
        }
    }
예제 #2
0
    //http://blog.building-blocks.com/uploading-images-using-the-core-service-in-sdl-tridion-2011
    public static void UploadImages(string location, string folderTcmId, CoreService2010Client client, log4net.ILog Log)
    {
        //create a reference to the directory of where the images are
            DirectoryInfo directory = new DirectoryInfo(location);
            //create global Tridion Read Options
            ReadOptions readOptions = new ReadOptions();
            //use Expanded so that Tridion exposes the TcmId of the newly created component
            readOptions.LoadFlags = LoadFlags.Expanded;
            try
            {
                //loop through the files
                foreach (FileInfo fileInfo in directory.GetFiles())
                {
                    //only allow images
                    if (IsAllowedFileType(fileInfo.Extension))
                    {
                        try
                        {
                            //create a new multimedia component in the folder specified
                            ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(Tridion.ItemType.Component, folderTcmId);
                            multimediaComponent.Title = fileInfo.Name.ToLower();
                            multimediaComponent.ComponentType = ComponentType.Multimedia;
                            multimediaComponent.Schema.IdRef = ConfigurationManager.AppSettings["MultimediaSchemaId"];

                             //create a string to hold the temporary location of the image to use later
                            string tempLocation = "";

                            //use the StreamUpload2010Client to upload the image into Tridion
                            UploadResponse us = new UploadResponse();
                            using (Tridion.StreamUpload2010Client streamClient = new StreamUpload2010Client())
                            {
                                FileStream objfilestream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
                                tempLocation = streamClient.UploadBinaryContent(fileInfo.Name.ToLower(), objfilestream);
                            }

                            //creat a new binary component
                            BinaryContentData binaryContent = new BinaryContentData();
                            //set this temporary upload location to the source of this binary
                            binaryContent.UploadFromFile = tempLocation;
                            binaryContent.Filename = fileInfo.Name.ToLower();

                            //get the multimedia type id
                            binaryContent.MultimediaType = new LinkToMultimediaTypeData() { IdRef = GetMultimediaTypeId(fileInfo.Extension) };

                            multimediaComponent.BinaryContent = binaryContent;

                            //save the image into a new object
                            IdentifiableObjectData savedComponent = client.Save(multimediaComponent, readOptions);

                            //check in using the Id of the new object
                            client.CheckIn(savedComponent.Id, null);
                        }
                        catch (Exception ex)
                        {
                            Log.Debug("Error creating image " + fileInfo.Name, ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error processing images", ex);
            }
            finally
            {
                //clean up temp objects
            }
    }