Library Extraction Code Updated

Posted by Community Admin on 03-Aug-2018 11:10

Library Extraction Code Updated

All Replies

Posted by Community Admin on 01-Jun-2010 00:00

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;

Posted by Community Admin on 01-Jun-2010 00:00

Sorry moderatos, this should have gone in the 3.X forum section, not in 4.X, i will pay more attention next time :)

This thread is closed