从服务器上传图片到 sitecore 媒体库

Upload images to sitecore media library from server

如何从服务器上的文件夹将图片上传到 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.

要将文件添加到媒体库,请使用此方法:

  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/

您也可以考虑使用 Sitecore PowerShell Extensions 来执行此操作。

https://himadritechblog.wordpress.com/2015/05/02/bulk-loading-images-in-sitecore-media-library-using-sitecore-powershell-extension-spe/