Copied to clipboard

Flag this post as spam?

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


  • Neil Hodges 338 posts 987 karma points
    Jun 30, 2015 @ 14:27
    Neil Hodges
    0

    Upload images to Media Folder from front end

    Hi

    Struggling a bit to get my head around uploading to the media folder. There are a few ways I've seen on the forums but i seem to be hitting a lot of Obsolete methods. has any one got an up to date example on uploading images to a specific Media Folder?

    My Html is:

     using (Html.BeginUmbracoForm<ArtworkUploadSurfaceController>("HandleArtworkUploadForm", null, new { @class = "artworkform" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)
                <input type="file" name="artworkfile" id="artworkfile" multiple />
                <br />
                <input type="submit" value="Upload" class="btn btn-primary btn-submit" />
    
            }
    

    and my Surface Controller is:

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult HandleArtworkUploadForm(ArtworkUploadViewModel model)
            {
                //Check if data posted from form in model is valid
                if (!ModelState.IsValid)
                {
                    //Not valid return to the form with the values the user entered
                    TempData["artworkUploadFail"] = true;
                    return CurrentUmbracoPage();
                }
    
                //get logged in user properties for upload folder
                var mem = Members.GetCurrentMember();
                var folder = mem.GetPropertyValue("artworkFolderName");
    
                //upload file to media folder
                if ((string) folder != string.Empty)
                {
                    //first check if folder exists for member, if not create it
    
                    //iterate over images and insert into media folder
    
                }
    
    
                //Update success flag (in a TempData key)
                TempData["artworkUploadSuccess"] = true;
                //All done - lets redirect to the current page & show our thanks/success message
                return RedirectToCurrentUmbracoPage();
            }
    

    You can see from my comments what I'm trying to do :) Any help would be greatly appreciated.

  • Ahsinah 18 posts 71 karma points
    Jan 27, 2016 @ 10:13
    Ahsinah
    0

    Hello Neil

    Did you manage to find a solution? I am trying to do something similar.

    Thanks Ahsinah

  • Neil Hodges 338 posts 987 karma points
    Jan 27, 2016 @ 10:22
    Neil Hodges
    0

    Hi Ahsinah

    I did get something working well, I'll post my code up on here so you can use the same or just strip out the bits that are relevant to you.

    Cheers Neil.

  • Neil Hodges 338 posts 987 karma points
    Jan 27, 2016 @ 10:27
    Neil Hodges
    4

    So here is the code from my surface controller, it uses a folder name set in the member properties to state the a folder to upload to but i guess you can make this static if you didn't want admin to specify a folder.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult HandleArtworkUploadForm(ArtworkUploadViewModel model)
        {
            //Check if data posted from form in model is valid
            if (!ModelState.IsValid)
            {
                //Not valid return to the form with the values the user entered
                TempData["artworkUploadFail"] = true;
                return CurrentUmbracoPage();
            }
    
            //get logged in user properties for upload folder
            var mem = Members.GetCurrentMember();
            var folder = mem.GetPropertyValue("artworkFolderName");
    
            //upload file to media folder
            if ((string) folder != string.Empty)
            {
                if (ApplicationContext.Current != null)
                {
                    var ms = ApplicationContext.Current.Services.MediaService;
                    var childfolder = ms.GetById(1135);//1135 is upload folder in media
    
                    if (childfolder.IsValid())
                    {
    
                        var foldermatch = false;
                        var foldermatchid = -1;
                        foreach (var memfolder in childfolder.Children().Where(x => x.ContentType.Alias == "Folder"))
                        {
                            if (memfolder.Name.ToLower() == folder.ToString().ToLower())
                            {
                                foldermatch = true;
                                foldermatchid = memfolder.Id;
                                break;
                            }
                        }
    
                        if (foldermatch)
                        {
                            //there is a members folder
                            var isimg = IsImage(model.Artworkfile);
                            var isfile = IsFile(model.Artworkfile);
    
                            if (isimg)
                            {
                                //save as image
                                var mediaService = Services.MediaService;
                                var media1 = mediaService.CreateMedia(model.Artworkfile.FileName, foldermatchid, "Image");
                                mediaService.Save(media1);
    
                                media1.SetValue("umbracoFile", model.Artworkfile);
                                mediaService.Save(media1);
    
                                //Update success flag (in a TempData key)
                                TempData["artworkUploadSuccess"] = true;
    
                            }
                            else if (isfile)
                            {
                                //save as file
                                var mediaService = Services.MediaService;
                                var media2 = mediaService.CreateMedia(model.Artworkfile.FileName, foldermatchid, "File");
                                mediaService.Save(media2);
    
                                media2.SetValue("umbracoFile", model.Artworkfile);
                                mediaService.Save(media2);
    
                                //Update success flag (in a TempData key)
                                TempData["artworkUploadSuccess"] = true;
                            }
                            else
                            {
                                //mime type not supported, don't upload
                                TempData["artworkUploadFail"] = true;
                            }
    
                        }
                        else
                        {
                            //there is no members folder, create one
                            var mediaFolder = ms.CreateMedia(folder.ToString(), 1135, "Folder");
                            ms.Save(mediaFolder);
    
                            //check type of file - image or file
                            var isimg = IsImage(model.Artworkfile);
                            var isfile = IsFile(model.Artworkfile);
    
                            if (isimg)
                            {
                                //save as image
                                var mediaService = Services.MediaService;
                                var media = mediaService.CreateMedia(model.Artworkfile.FileName, mediaFolder, "Image");
                                mediaService.Save(media);
    
                                media.SetValue("umbracoFile", model.Artworkfile);
                                mediaService.Save(media);
    
                                //Update success flag (in a TempData key)
                                TempData["artworkUploadSuccess"] = true;
                            }
                            else if (isfile)
                            {
                                //save as file
                                var mediaService = Services.MediaService;
                                var media = mediaService.CreateMedia(model.Artworkfile.FileName, mediaFolder, "File");
                                mediaService.Save(media);
    
                                media.SetValue("umbracoFile", model.Artworkfile);
                                mediaService.Save(media);
    
                                //Update success flag (in a TempData key)
                                TempData["artworkUploadSuccess"] = true;
                            }
                            else
                            {
                                //mime type not supported, don't upload
                                TempData["artworkUploadFail"] = true;
                            }
                        }
                    }
    
                }
    
            }
    
            //All done - lets redirect to the current page & show our thanks/success message
            return RedirectToCurrentUmbracoPage();
        }
    
        private bool IsImage(HttpPostedFileBase file)
        {
            string[] formats = new string[] { ".jpg", ".png", ".gif", ".jpeg" }; 
    
            return formats.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase));
        }
    
        private bool IsFile(HttpPostedFileBase file)
        {
            string[] formats = new string[] { ".pdf", ".psd", ".ai" };
    
            return formats.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase));
        }
    

    Hope this helps you.

    Cheers Neil.

Please Sign in or register to post replies

Write your reply to:

Draft