【问题标题】:Upload images to sitecore media library from server从服务器上传图片到 sitecore 媒体库
【发布时间】:2016-01-13 14:10:48
【问题描述】:

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

【问题讨论】:

    标签: sitecore sitecore7.2


    【解决方案1】:

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

      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);`
    • 我更新了我的答案,正在从给定文件创建 mediaitem。
    • 我正在使用您的代码。但什么都没有发生。我也更新了我的问题。你能检查一下吗?
    • 如果正在调用我的方法,您是否检查过调试?
    • 现在可以工作了。我弄错了。我会在稍后更新问题。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 2012-01-29
      • 2013-01-29
      • 2020-08-31
      • 1970-01-01
      • 2014-06-08
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多