Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christian Hansen 34 posts 204 karma points
    Jul 24, 2019 @ 18:57
    Christian Hansen
    0

    Add image to contentService creation.

    Hello, i have a page with a form. I am using the contentService to create content in Umbraco. I have only 2 fields, a Header and a description field. How can i in that form also upload a file and save it on the node the contentservice are creating. I have a mediapicker property on the node i am creating through the content service. Do i have to make a upload with the mediaService first og can i do the both in one action?

    I now have this in my controller:

    public class ContactFormSurfaceController : SurfaceController
    {
        // GET: ContactFormSurface
        public ActionResult Index()
        {
            return PartialView("ContactForm", new ContactFormViewModel());
        }
    
        [HttpPost]
        public ActionResult HandleFormSubmit(ContactFormViewModel model)
        {
            //
            if (!ModelState.IsValid)
                return CurrentUmbracoPage();
    
            //Create content
            var newRecord = Services.ContentService.CreateContent(model.PostName, CurrentPage.Id, "federationRecordPage");
            newRecord.SetValue("header", model.PostName);
            newRecord.SetValue("text", model.Text);
    
            Services.ContentService.SaveAndPublishWithStatus(newRecord);
    
            TempData["success"] = true;
            return RedirectToCurrentUmbracoPage();
        }
    }
    

    My model:

    public class ContactFormViewModel
    {
        [Required]
        public string PostName { get; set; }
        [Required]
        public string Text { get; set; }
    
        public int GalleryId { get; set; }
    
        public IEnumerable<HttpPostedFileBase> Files { get; set; }
    }
    

    i have tried to follow the media service guide on UmbracoTv.

  • Josip 195 posts 662 karma points c-trib
    Jul 25, 2019 @ 09:06
    Josip
    1

    Hello Christian, do you want to save that image in media section of Umbraco or just on disk?

    This is how i do id:

    // Iniitialize new item

    var newItem = Services.ContentService.Create(title, folderId, "newsItem");  
    

    // Upload the file to the server and Umbraco media

    IMedia uploadFile = Services.MediaService.CreateMediaWithIdentity(file.FileName, mediaFolderId, "Image");
     uploadFile.SetValue(Services.ContentTypeBaseServices, "umbracoFile", file.FileName, file.InputStream);
    Services.MediaService.Save(uploadFile);
    

    // Set Media picker value

    newItem.SetValue("image", uploadFile.GetUdi().ToString());
    Services.ContentService.SaveAndPublish(newItem);
    
  • Christian Hansen 34 posts 204 karma points
    Jul 25, 2019 @ 09:10
    Christian Hansen
    0

    Hello Josip, i want to save it in the media section of Umbraco yes.

    Thanks for the reply i will try that.

  • Christian Hansen 34 posts 204 karma points
    Jul 26, 2019 @ 08:22
    Christian Hansen
    0

    Hello Josip,

    I have now tried this.

    [HttpPost]
        public ActionResult HandleFormSubmit(ContactFormViewModel model)
        {
            //
            if (!ModelState.IsValid)
                return CurrentUmbracoPage();
    
            //Create content
            var newRecord = Services.ContentService.CreateContent(model.PostName, CurrentPage.Id, "federationRecordPage");
    
            IMedia uploadFile = Services.MediaService.CreateMediaWithIdentity("Test123", 117, "Image");
            uploadFile.SetValue(Services.ContentTypeService, "umbracoFile", "NameOfFile", model.Files);
            Services.MediaService.Save(uploadFile);
    
            newRecord.SetValue("documentUpload", uploadFile.GetUdi().ToString());
    
            newRecord.SetValue("header", model.PostName);
            newRecord.SetValue("text", model.Text);
    
            Services.ContentService.SaveAndPublishWithStatus(newRecord);
    
            TempData["success"] = true;
            return RedirectToCurrentUmbracoPage();
        }
    }
    

    I cannot use the "Services.ContentTypeBaseServices" only "Services.ContentTypeService" but it says it cannot take 4 arguments.

    Can you help a little more?

  • Josip 195 posts 662 karma points c-trib
    Jul 26, 2019 @ 12:58
    Josip
    0

    Hi Cristian, now when you said that i checked and i see that you are asking for Umbraco 7 and my solution is for Umbraco 8.

    I am not sure how to do it in Umbraco 7 because i worked with content service only in Umbraco 8 :(

    But you have tutorial on Umbraco tv.

    This is how to upload the image to the media section: https://umbraco.tv/videos/umbraco-v7/developer/fundamentals/media-api/

    And if you want to set value for media picker property , you should know thay you need guid of the image to set value for media picker.

    If you dont find the solution, write me back and i will try to do it in Umbraco 7.

    BR

    Josip

  • Christian Hansen 34 posts 204 karma points
    Aug 06, 2019 @ 18:16
    Christian Hansen
    100

    I got it to work. Solution:

    IMedia uploadMediaFiles = Services.MediaService.CreateMedia(file.FileName, MemberMediaFolder, FileType);
                uploadMediaFiles.SetValue("umbracoFile", file);
                Services.MediaService.Save(uploadMediaFiles);
    
                gallerySavedPics += uploadMediaFiles.GetUdi().ToString() + ",";
    
    gallerySavedPics = gallerySavedPics.Substring(0, gallerySavedPics.Length - 1);
    
            if (gallerySavedPics != "")
            {
    
                newRecord.SetValue("documentUpload", gallerySavedPics);
            }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies