如何从服务器上的文件夹上传图像到Sitecore媒体库
var dir = new DirectoryInfo(MapPath("~/images/temp"));
FileInfo[] files = dir.GetFiles();
var list = new ArrayList();
foreach (FileInfo file in files)
{
if (file.Extension == ".jpg" || file.Extension == ".jpeg" || file.Extension == ".gif" || file.Extension == ".png")
{
list.Add(file);
}
}
// Now I want to add the files to media library in sitecore.
答案 0 :(得分:3)
要将文件添加到媒体库,请使用以下方法:
public MediaItem AddFile(string fileName, string sitecorePath, string mediaItemName)
{
// Create the options
Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions();
// Store the file in the database, not as a file
options.FileBased = false;
// Remove file extension from item name
options.IncludeExtensionInItemName = false;
// Overwrite any existing file with the same name
options.KeepExisting = false;
// Do not make a versioned template
options.Versioned = false;
// set the path
options.Destination = sitecorePath + "/" + mediaItemName;
// Set the database
options.Database = Sitecore.Configuration.Factory.GetDatabase("master");
// Now create the file
Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator();
MediaItem mediaItem = creator.CreateFromFile(filename, options);
return mediaItem;
}
您将调用此方法:
MediaItem myFile = AddFile("c:\\myfile.pdf", "myfile", "/pdffolder/uploaded");
在您的情况下,您将拥有:
var dir = new DirectoryInfo(MapPath("~/images/temp"));
FileInfo[] files = dir.GetFiles();
var list = new ArrayList();
foreach (FileInfo file in files)
{
if (file.Extension == ".jpg" || file.Extension == ".jpeg" || file.Extension == ".gif" || file.Extension == ".png")
{
AddFile(file.FullName, file.Name, "/pdffolder/uploaded");
}
}
您可以在此处找到详细信息: https://briancaos.wordpress.com/2009/07/09/adding-a-file-to-the-sitecore-media-library-programatically/
答案 1 :(得分:0)
您也可以考虑使用Sitecore PowerShell Extensions来执行此操作。