上的圖像,以Sitecore的媒體庫將文件添加到媒體庫中使用這種方法:
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/
感謝您的回答。這行是做什麼'MediaItem mediaItem = creator.CreateFromFile(「c:\\ myfile.pdf」,options);' – Kamran
我更新了我的答案,正在創建一個給定的文件mediaitem。 –
我正在使用您的代碼。但沒有任何事情發生。我也更新了我的問題。你可以檢查一下嗎? – Kamran