Skip to content

Instantly share code, notes, and snippets.

@trnktms
Last active October 7, 2016 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trnktms/7cb0286218a51341d39c649800013096 to your computer and use it in GitHub Desktop.
Save trnktms/7cb0286218a51341d39c649800013096 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using Sitecore.Data.Items;
using Sitecore.Resources.Media;
using Sitecore.SharedSource.DataImporter.Mappings.Fields;
using Sitecore.SharedSource.DataImporter.Providers;
namespace Sitecore.SharedSource.DataImporter.Mappings.Fields
{
public class ToMedia : ToText
{
public ToMedia(Item i)
: base(i)
{}
public override void FillField(BaseDataMap map, ref Item newItem, string importValue)
{
var bytes = Convert.FromBase64String(importValue);
var media = MediaManager.GetMedia(newItem);
// here upload the picture as a .jpg
media.SetStream(new MemoryStream(bytes, false), "jpg");
// it's needed to make it editable after SetStream becauase it closes the edit context
if (!newItem.Editing.IsEditing)
{
newItem.Editing.BeginEdit();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment