我试过这段代码,但无法使用 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();
}
}
}
}
我是一名优秀的程序员,十分优秀!