如何将媒体库图像从 sitecore 下载到本地文件夹
How to Download Media Library images from sitecore to local folder
我试过这段代码,但无法使用 From Using 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();
}
}
这是一个对我有用的例子:
//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();
}
}
}
}
}
Goose 的答案相当有效,但如果文件夹中有大量图像(200 多张),则需要改用 Sitecore 快速查询。
只需将 "fast:" 添加到 SelectItems 查询的开头,以便该行最终看起来像:
var images = database.SelectItems("fast:/sitecore/media library/Images/Branches/descendant::*[@@templatekey='jpeg']");
我试过这段代码,但无法使用 From Using 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();
}
}
这是一个对我有用的例子:
//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();
}
}
}
}
}
Goose 的答案相当有效,但如果文件夹中有大量图像(200 多张),则需要改用 Sitecore 快速查询。
只需将 "fast:" 添加到 SelectItems 查询的开头,以便该行最终看起来像:
var images = database.SelectItems("fast:/sitecore/media library/Images/Branches/descendant::*[@@templatekey='jpeg']");