/// <summary> /// Save image in the Media library /// </summary> /// <param name="stream"></param> /// <param name="fileName"></param> /// <param name="path"></param> /// <param name="mediaItemName"></param> /// <returns></returns> private bool UploadMediaItem(Stream stream, string fileName, string path, string mediaItemName) { if (string.IsNullOrWhiteSpace(fileName) || string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(mediaItemName)) { Sitecore.Diagnostics.Log.Error($"Could not upload media item either filename, path or mediaitemname is null.", this); return(false); } if (stream == Stream.Null) { Sitecore.Diagnostics.Log.Error($"Could not upload stream is null for media item {mediaItemName}.", this); return(false); } string destination = $"{path}/{mediaItemName}"; try { Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions() { FileBased = false, IncludeExtensionInItemName = false, OverwriteExisting = true, Versioned = false, Destination = destination, Database = Sitecore.Configuration.Factory.GetDatabase(masterDb), AlternateText = mediaItemName }; using (new SecurityDisabler()) { Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); MediaItem mediaItem = creator.CreateFromStream(stream, fileName, options); if (mediaItem != null) { Log.Info($"Media Item {destination} uploaded successfully.", this); return(true); } } } catch (Exception ex) { Log.Error($"Error uploading Media Item {destination}", ex, this); } finally { stream.Dispose(); } return(false); }
public MediaItem CreateMedaiItem(Stream stream, string fileName, string sitecorePath) { Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions() { FileBased = false, IncludeExtensionInItemName = false, OverwriteExisting = false, Versioned = false, Destination = sitecorePath + "/" + Path.GetFileNameWithoutExtension(fileName), Database = Sitecore.Configuration.Factory.GetDatabase("master") }; Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); MediaItem mediaItem = creator.CreateFromStream(stream, fileName, options); return(mediaItem); }
/// <summary> /// save image inside media gallery /// </summary> /// <param name="image">Image</param> /// <param name="fileName">File name</param> /// <param name="alternateText">Alternate text image</param> public static void SaveImage(Bitmap image, string fileName, string alternateText) { // Create memory stream object var memoryStream = new MemoryStream(); image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); // save image in Media galley var options = new Sitecore.Resources.Media.MediaCreatorOptions { FileBased = false, OverwriteExisting = true, Versioned = true, IncludeExtensionInItemName = true, Destination = fileName, Database = Factory.GetDatabase("master"), AlternateText = alternateText }; using (new SecurityDisabler()) { var creator = new Sitecore.Resources.Media.MediaCreator(); creator.CreateFromStream(memoryStream, fileName, options); } }
/// <summary> /// Add the image to Sitecore /// </summary> /// <param name="percolatePost"></param> /// <returns></returns> public MediaItem CreateMedia(PercolatePost percolatePost) { //Create the options Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); options.FileBased = false; options.IncludeExtensionInItemName = false; options.KeepExisting = false; options.Versioned = false; options.Destination = PercolateConstants.Paths.MediaPath + percolatePost.Media.Id; options.Database = Sitecore.Configuration.Factory.GetDatabase(Settings.PercolateSettings.MasterDBName); //read the image from percolate var webClient = new WebClient(); byte[] imageBytes = webClient.DownloadData(percolatePost.Media.Src); MemoryStream stream = new MemoryStream(); stream.Write(imageBytes, 0, imageBytes.Length); //create the image Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); MediaItem mediaItem = creator.CreateFromStream(stream, string.Format("{0}.{1}", percolatePost.Media.Id, percolatePost.Media.Format), options); return mediaItem; }
public ActionResult UploadImage() { bool isSavedSuccessfully = true; string fName = ""; try { using (new SecurityDisabler()) { foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; //Save file content goes here if (file != null && file.ContentLength > 0) { var tempStream = new MemoryStream(); file.InputStream.CopyTo(tempStream); tempStream.Seek(0, SeekOrigin.Begin); fName = file.FileName; var newFileName = Guid.NewGuid() .ToString() .Replace("-", string.Empty) .Replace("{", string.Empty) .Replace("}", string.Empty); var imagesRootId = ID.Parse(Settings.GetSetting("ImagesRoot")); var imagesRoot = Sitecore.Context.Database.GetItem(imagesRootId); // 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; options.IncludeExtensionInItemName = false; // set the path options.Destination = imagesRoot.Paths.FullPath + "/" + newFileName; // 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.CreateFromStream(tempStream, newFileName + Path.GetExtension(file.FileName), options); PublishManager.PublishItem(mediaItem, new[] { Database.GetDatabase("web"), }, new[] { Language.Current }, false, false); CorePipeline.Run("detectFaces", new DetectFacesArgs(mediaItem.InnerItem)); } } } } catch (Exception ex) { isSavedSuccessfully = false; } if (isSavedSuccessfully) { return(Json(new { Message = fName })); } return(Json(new { Message = "Error in saving file" })); }