More in this section
Blogs RSS feed

Save Images from the Images and Documents module back to the file system

by User Not Found
When you upload images from your file system to Sitefinity using Images and Documents module all items go to the database as binary object. In some cases you may need to get this Images back to your file system - upload them in another project, back up on the server, user server system as storage instead of the database due to some limits. The built-in implementation of the module does not expose similar option, so I decided to write a simple code for this.

Using Sitefinity's API, I get all images from a given library. Each IContent object of this library is an image, so the content of this IContent object is actually the buffer I need to build an image using System.Drawing.Image class.

                 string storage = "C:\\Projects\\Sitefinity\\Images\\"
            var man = new LibraryManager(); 
            ILibrary lib = man.GetLibrary("ImageGallery"); 
            IList allImages = lib.GetItems(); 
            foreach (IContent contentItem in allImages) 
                int i = 0; 
                IContent cnt = man.GetContent(contentItem.ID); 
                string name = (string)cnt.GetMetaData("Name"); 
                string ext = (string)cnt.GetMetaData("Extension");  
                byte[] buffer1 = (byte[])cnt.Content; 
                System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(buffer1)); 
                img.Save(storage + name + ext, ImageFormat.Jpeg); 
            Response.Write(i.ToString() + "-" + "items have been uploaded to:" + storage); 

Here all my images are jpeg, but you can add switch loop for all common formats - jpeg, png, bmp, so that the items will be stored with the same extension to your file system.


Leave a comment
  1. Neil Feb 01, 2010
    Does this approach still support batch update and editing of images through the administrion area.

    Regards, Neil
  2. Ivan Dimitrov Feb 01, 2010
    This gets the items back to the file system. The code does not have to do anything with batch edit.
  3. KingKong Feb 01, 2010
    Ivan, thank you for the great article. Just tried on my project, works like a charm
  4. Tim Feb 02, 2010
    Ivan, I have a no-code method of a accomplishing the same type of thing:

    1. In Sitefinity, create a page.
    2. Add a library control.
    3. Configure library control show all images on one page.
    4. Preview page in new window in IE.
    5. File > Save As (Web Page, Complete)

    All of your images will be in the /files folder.

    It's not as elegant as your solution, but it's a quick way to extract the images for use in another site or project.

  5. Ivan Dimitrov Feb 02, 2010
    It is possible, but you can add this as a built-in tool in Sitefinity. So it will be a feature and each user will be able to use it easily.
  6. Jorge Feb 05, 2010
    Hello Ivan,

    Is this approach also available if we are using amazon s3 as storage for our images?

    This could be great because I don't find another approach for backing up all the data stored there.

    Thanks foy your post!
  7. Georgi Feb 11, 2010

    We haven't tried this but conceptually it should work. Basically it's not important from where do you take the items, once the provider "provides" you with the data, you can manipulate it - like in our case -  save it on the file system. 
  8. Mike Sharp Feb 19, 2010
    I thought I'd read somewhere in the past year that you supported upload to S3 beginning in SF 3.6...or was that simply wishful thinking on my part?   ;^)

    Mike Sharp
  9. Georgi Feb 22, 2010
    The feature is implemented in 3.6 and 3.7 :)
  10. Rob Modica Jun 01, 2010
    Another version to include all libraries and all files

    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);
                Response.Write(i.ToString() + "-" + "items have been uploaded to:" + storage + "<br/>");
    public bool writeByteArrayToFile(byte[] buff, string fileName)
        bool response = false;
            FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Close(); //Thanks Karlo for pointing out!
            response = true;
        catch (Exception ex)
        return response;
  11. Jenny123 Apr 18, 2018
    This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck! 
    Bitdefender Support

    Leave a comment