Srinu A
Srinu A

Reputation: 21

How to download Sitecore Images

var database = Database.GetDatabase(“master”);
var images = database.SelectItems(“/sitecore/media library/your image folder/descendant::*[@@templatekey=’jpeg’]”);

foreach (var image in images)
{
var mediaItem = (MediaItem)image;
var media = MediaManager.GetMedia(mediaItem);
var stream = media.GetStream();

using (var targetStream = File.OpenWrite(Path.Combine(“your data path”, image.Name + “.jpeg” )))
{
stream.CopyTo(targetStream);
targetStream.Flush();
}
}

I'm new to Sitecore, can anyone help me in how to download images from Sitecore to the local folder in our PC and when I searched I found a code but not working. The error is Could not find a part of the path 'E:\Images\RedTorch.jpeg'

Upvotes: 0

Views: 1730

Answers (2)

Marek Musielak
Marek Musielak

Reputation: 27132

Here is a nice blog post describing how to download images from particular folder in media library: Save all images of a media library folder to disk.

IMPORTANT: remember that user which is used by the IIS process must have write access to the c:\tmp directory (or any other directory you use).

And the code from there:

var database = Database.GetDatabase("master");
var images = database.SelectItems("/sitecore/media library/your image folder/descendant::*[@@templatekey='jpeg']");

foreach (var image in images)
{
    var mediaItem = (MediaItem)image;
    var media = MediaManager.GetMedia(mediaItem);
    var stream = media.GetStream();

    using (var targetStream = File.OpenWrite(Path.Combine("c:\\tmp", image.Name + ".jpeg" )))
    {
        stream.CopyTo(targetStream);
        targetStream.Flush();
    }
}

EDIT:

From your comments it looks like instead of saving media items on the server hard drive, you want to download them to the computer which is used to browse the site. Is that correct?

In that scenario, you can use code below. It will generate a zip of all media items which are direct children on the selected folder and allow user to download this zip:

string tempFolderPath = Sitecore.Configuration.Settings.TempFolderPath;
tempFolderPath = HttpContext.Current.Server.MapPath(tempFolderPath);
string zipName = "media" + DateTime.Now.Ticks + ".zip";
ZipWriter zipWriter = new ZipWriter(Path.Combine(tempFolderPath, zipName));

var database = Database.GetDatabase("master");
var images = database.SelectItems("/sitecore/media library/your folder/*");

foreach (var image in images)
{
    if (MediaManager.HasMediaContent(image))
    {
        var mediaItem = (MediaItem) image;
        var media = MediaManager.GetMedia(mediaItem);
        var stream = media.GetStream();

        zipWriter.AddEntry(mediaItem.Name + "." + mediaItem.Extension, stream.Stream);
    }
}

zipWriter.Dispose();

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("inline;filename=\"{0}\"", zipName));
HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.OK;
HttpContext.Current.Response.BufferOutput = true;

using (StreamReader sr = new StreamReader(Path.Combine(tempFolderPath, zipName)))
{
    sr.BaseStream.CopyTo(HttpContext.Current.Response.OutputStream);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
}

Upvotes: 1

Rob Habraken
Rob Habraken

Reputation: 499

The code from your example looks okay and is roughly the same as Marek's answer, what might be the problem here is that you are using Path.Combine to include a new folder that probably does not exist yet (based on the error message you get: "Could not find a part of the path").

If you will add this line just before writing to the file (create a variable first, holding your final and complete path string), you can be sure that all directories exist before writing the file to disk:

Directory.CreateDirectory(Path.GetDirectoryName(filePath));

Upvotes: 0

Related Questions