Sitefinity ASP.NET CMS

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

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); 
                i++; 
             
            } 
            Response.Write(i.ToString() + "-" + "items have been uploaded to:" + storage); 

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

Tags: 3.7 Modules content items API

By Ivan Dimitrov on 31 Jan 2010 in Gotchas All Blog Posts

  • Facebook
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Furl it!

9  comments

  • Neil
    01 Feb 2010
    Does this approach still support batch update and editing of images through the administrion area.

    Regards, Neil
  • Ivan Dimitrov
    01 Feb 2010
    This gets the items back to the file system. The code does not have to do anything with batch edit.
  • KingKong
    01 Feb 2010
    Ivan, thank you for the great article. Just tried on my project, works like a charm
  • Tim
    01 Feb 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.

  • Ivan Dimitrov
    02 Feb 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.
  • Jorge
    05 Feb 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!
  • Georgi
    11 Feb 2010
    Jorge,

    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. 
  • Mike Sharp
    19 Feb 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?   ;^)

    Regards,
    Mike Sharp
  • Georgi
    22 Feb 2010
    Mike,
    The feature is implemented in 3.6 and 3.7 :)
Add Comment
  1. Formatting options
       
      
     
     
       
  2. Security image

Subscribe to blog feed

Categories

About Telerik

Telerik, the publisher of Sitefinity CMS, is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting and .NET ORMTFSCode Analysis and Web Application Testing tools. Building on its solid expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Created with passion, Telerik products help thousands of developers every day to be more productive and deliver reliable applications under budget and on time. Read more about Telerik

Copyright © 2002-2010 Telerik. All rights reserved. Powered by Sitefinity