Copied to clipboard

Flag this post as spam?

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


  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Dec 12, 2016 @ 16:41
    Frans de Jong
    0

    Media upload via api

    Hi all,

    I'm using a mediaupload in a doctype. The same doctype is used in the frontend. In the Umbraco course we only learned how to make a new folder and add a image to that folder but I don't want a folder.

    What should I use codewise? My current code looks like this:

     if ((model.NewNewsImage != null) && (model.NewNewsImage.ContentLength != 0) && (model.NewNewsImage.ContentType.Contains("image")))
                {
                    PublishedPropertyType imagesProperty = NewsItem.GetModelPropertyType(n => n.Image);
                    int folderId = currentNewsItem.GetValue<int>(imagesProperty.PropertyTypeAlias);
                    if (folderId <= 0)
                    {
                        IMedia folder = mediaService.CreateMedia(model.Content.DocumentTypeAlias, -1, Folder.ModelTypeAlias);
                        mediaService.Save(folder);
                        folderId = folder.Id;
                    }
    
                    IMedia mediaImage = mediaService.CreateMedia(model.NewNewsImage.FileName, folderId, Image.ModelTypeAlias);
                    var umbracoFileProperty = Image.GetModelPropertyType(i => i.UmbracoFile);
                    mediaImage.SetValue(umbracoFileProperty.PropertyTypeAlias, model.NewNewsImage);
                    mediaService.Save(mediaImage);
                    currentNewsItem.SetValue("image", mediaImage.GetValue("umbracoFile"));
    
                }
    

    How can I modify this code to not using folders?

    Frans

  • Alex Skrypnyk 6148 posts 24097 karma points MVP 8x admin c-trib
    Dec 12, 2016 @ 23:55
    Alex Skrypnyk
    100

    Hi Frans,

    You can only use root folder or a separate folder.

    You haven't other choices, why you don't want folders?

    Thanks,

    Alex

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Dec 13, 2016 @ 08:43
    Frans de Jong
    0

    Hi Alex,

    Thanks for your answer. In the backoffice we have a image upload (media cropper) that doesn't store images in a folder. On the site we have the same template and we want the same behavior with the images if possible.

    Frans

Please Sign in or register to post replies

Write your reply to:

Draft