如何将媒体库映像从sitecore下载到本地文件夹

时间:2015-02-04 12:38:11

标签: c# sitecore sitecore6

我尝试了这段代码,但没有使用From Statement。

var database = Sitecore.Configuration.Factory.GetDatabase("master");
var images = database.SelectItems("/sitecore/media library/Corsair/Home Page/*");
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(@"F:\folder", image.Name + ".jpeg" )))
       {
           stream.CopyTo(targetStream);
           targetStream.Flush();
       }
     } 

2 个答案:

答案 0 :(得分:0)

这是一个适合我的例子:

//button to trigger event on front end
<asp:Button ID="myButton" Text="Export Images" OnClick="ExportMedia" runat="server" />

中的代码

using System;
using System.IO;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Resources.Media;

namespace YourNamespace
{
public partial class MediaLibraryExporter : System.Web.UI.Page
{
    public void ExportMedia(object sender, EventArgs e)
    {
        var database = Database.GetDatabase("master");
        var images = database.SelectItems("/sitecore/media library/Images/Branches/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 output folder location", image.Name + ".jpeg" )))
            {
                stream.CopyTo(targetStream);
                targetStream.Flush();
            }
        }
    }

}

}

答案 1 :(得分:0)

Goose的答案效果相当不错,但如果文件夹中有很多图片(200+),则需要使用Sitecore Fast Query。

只需将“fast:”添加到SelectItems查询的开头,以便该行最终看起来像:

var images = database.SelectItems("fast:/sitecore/media library/Images/Branches/descendant::*[@@templatekey='jpeg']");