More in this section
Categories
Bloggers
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); 
                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 common formats - jpeg, png, bmp, so that the items will be stored with the same extension to your file system.

24 comments

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
    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. 
  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?   ;^)

    Regards,
    Mike Sharp
  9. Georgi Feb 22, 2010
    Mike,
    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);
                    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;
    }
  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
  12. robin Jun 20, 2018
    I read your article. It is very informative and helpful for everyone.norton support number
  13. sarahjackvp Jun 21, 2018
    Excellent and very exciting site. Love to watch.Outlook Contact Number
  14. MONIKA Jul 05, 2018
  15. MONIKA Jul 06, 2018
    Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanksmcse training london
  16. MONIKA Jul 06, 2018
    Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me.buy cryptocurrencies offshore
  17. MONIKA Jul 06, 2018
    All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanksgenerator maintenance
  18. MONIKA Jul 06, 2018
    Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.pakej percutian
  19. MONIKA Jul 06, 2018
    I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot!engagement ring singapore
  20. MONIKA Jul 06, 2018
    Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!!Derek Lam 10 Crosby apparel
  21. okli Jul 10, 2018
    You have managed to design the room so wonderfully. They are doing a pretty good job to keep the floor so neat and clean. This will be a nice place to sit and relax during the hour-long sessions of lectures.HP Uk Support<
  22. MONIKA Jul 12, 2018
    Hi! Thanks for the great information you havr provided! You have touched on crucuial points!read more
  23. nockjones Jul 17, 2018
     Thanks for the valuable information and insights you have so provided here  Acer Customer Service
  24. Annett Dresdner Jul 18, 2018
    Thanks for your information, it was really very helpfull..Best minoxidil for hair loss

    Leave a comment