public void HowToSetAndGetBlobStream() { // arrange var stream = new System.IO.MemoryStream(); using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db { new Sitecore.FakeDb.DbItem("home") { new Sitecore.FakeDb.DbField("field") } }) { Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/home"); Sitecore.Data.Fields.Field field = item.Fields["field"]; using (new Sitecore.Data.Items.EditContext(item)) { // act field.SetBlobStream(stream); } // assert Xunit.Assert.Equal(stream.ToArray(), ((System.IO.MemoryStream)field.GetBlobStream()).ToArray()); } }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { var data = field.GetBlobStream(); MemoryStream stream = null; if (data.CanRead) { stream = new MemoryStream(); byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = data.Read(buffer, 0, buffer.Length)) > 0) { stream.Write(buffer, 0, bytesRead); } data.Close(); stream.Seek(0, SeekOrigin.Begin); } return(stream); }
/// <summary> /// Gets the field. /// </summary> /// <param name="field">The field.</param> /// <param name="config">The config.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) { return(field.GetBlobStream()); }