More in this section

Forums / General Discussions / Library Extraction Code Updated

Library Extraction Code Updated

2 posts, 0 answered
  1. Roberto
    Roberto avatar
    146 posts
    Registered:
    08 Feb 2008
    01 Jun 2010
    Link to this post
    An updated version of the blog post Save Images from the Images and Documents module back to the file system post which allows for all libraries and all files to be processed, thought it may be useful for people if they are doing the same migration work we are.

    Rob 

    protected void cmdExtract_Click(object sender, EventArgs e)
    {
        string storage = "C:\\Source\\Pan\\Library\\";
        var man = new LibraryManager();
        IList libraries = man.GetAllLibraries();
        foreach (ILibrary l in libraries)
        {
                ILibrary lib = l;
                IList allImages = lib.GetItems();
                int i = 0;
                foreach (IContent contentItem in allImages)
                {
                    IContent cnt = man.GetContent(contentItem.ID);
                    string name = (string)cnt.GetMetaData("Name");
                    string ext = (string)cnt.GetMetaData("Extension");
                    byte[] buffer1 = (byte[])cnt.Content;
                    writeByteArrayToFile(buffer1, storage + name + ext);
                    i++;
                }
                Response.Write(i.ToString() + "-" + "items have been uploaded to:" + storage + "<br/>");
        }
    }
    public bool writeByteArrayToFile(byte[] buff, string fileName)
    {
        bool response = false;
        try
        {
            FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(buff);
            bw.Close(); //Thanks Karlo for pointing out!
            response = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return response;
    }
  2. Roberto
    Roberto avatar
    146 posts
    Registered:
    08 Feb 2008
    01 Jun 2010
    Link to this post
    Sorry moderatos, this should have gone in the 3.X forum section, not in 4.X, i will pay more attention next time :)
2 posts, 0 answered