private int GetParentId(Umbraco.Forms.Core.Field AssociatedField) { var parentId = -1; //media root :: node = -1 if (!this.MediaParentId.IsNullOrWhiteSpace()) { try { parentId = Convert.ToInt32(this.MediaParentId); } catch (Exception ex1) { var msg = string.Format("Provided property value for 'MediaParentId' on field '{0}' on Form is not a valid Integer.", AssociatedField.Alias); LogHelper.Error <MediaUpload>(msg, ex1); } } else if (!this.MediaParentPath.IsNullOrWhiteSpace()) { try { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var pathSplit = this.MediaParentPath.Split('>'); var mediaParent = umbracoHelper.TypedMediaAtRoot().Where(m => m.Name == pathSplit[0]).FirstOrDefault(); for (int i = 1; i < pathSplit.Length - 1; i++) { var nodeName = pathSplit[i]; mediaParent = mediaParent.Children.Where(m => m.Name == nodeName).FirstOrDefault(); } parentId = mediaParent.Id; } catch (Exception ex2) { var msg = string.Format("Provided property value for 'MediaParentPath' on field '{0}' on Form caused an error.", AssociatedField.Alias); LogHelper.Error <MediaUpload>(msg, ex2); } } return(parentId); }
public override IEnumerable <object> ProcessSubmittedValue(Umbraco.Forms.Core.Field AssociatedField, System.Collections.Generic.IEnumerable <object> Values, System.Web.HttpContextBase HttpContext) { List <Object> vals = new List <object>(); //files var ms = ApplicationContext.Current.Services.MediaService; var cts = ApplicationContext.Current.Services.ContentTypeService; bool filesaved = false; string _text; string mediaPath = ""; Media m = null; var files = HttpContext.Request.Files; if (files.Count > 0 && files.AllKeys.Contains(AssociatedField.Id.ToString())) { HttpPostedFileBase file = null; file = files[AssociatedField.Id.ToString()]; if (file.ContentLength > 0) { if (file.FileName != "") { // Find filename _text = file.FileName; string filename; string _fullFilePath; filename = _text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower(); // create the Media Node var mediaType = cts.GetMediaType(ImageMediaTypeId); var mediaParentId = this.GetParentId(AssociatedField); m = new Media( filename, mediaParentId, mediaType); ms.Save(m); // Create a new folder in the /media folder with the name /media/propertyid string mediaRootPath = "~/media/"; // get path from App_GlobalResources string storagePath = mediaRootPath + m.Id.ToString(); if (!System.IO.Directory.Exists(IOHelper.MapPath(storagePath))) { System.IO.Directory.CreateDirectory(IOHelper.MapPath(storagePath)); } _fullFilePath = IOHelper.MapPath(storagePath) + "\\" + filename; file.SaveAs(_fullFilePath); // Save extension string orgExt = ((string)_text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1)); orgExt = orgExt.ToLower(); string ext = orgExt.ToLower(); try { m.SetValue("umbracoExtension", ext); } catch {} // Save file size try { System.IO.FileInfo fi = new System.IO.FileInfo(_fullFilePath); m.SetValue("umbracoBytes", fi.Length.ToString()); } catch {} // Check if image and then get sizes, make thumb and update database if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + ext + ",") > 0) { int fileWidth; int fileHeight; FileStream fs = new FileStream(_fullFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); System.Drawing.Image image = System.Drawing.Image.FromStream(fs); fileWidth = image.Width; fileHeight = image.Height; fs.Close(); try { m.SetValue("umbracoWidth", fileWidth.ToString()); m.SetValue("umbracoHeight", fileHeight.ToString()); } catch {} // Generate thumbnails string fileNameThumb = _fullFilePath.Replace("." + orgExt, "_thumb"); GenerateThumbnail(image, 100, fileWidth, fileHeight, _fullFilePath, ext, fileNameThumb + ".jpg"); image.Dispose(); } mediaPath = "/media/" + m.Id.ToString() + "/" + filename; m.SetValue("umbracoFile", mediaPath); ms.Save(m); vals.Add(m.Id); filesaved = true; } } } if (!filesaved) { vals.Add("No file saved"); } return(vals); }