Copied to clipboard

Flag this post as spam?

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


  • José Oliveira 1 post 71 karma points
    Jul 18, 2018 @ 17:40
    José Oliveira
    0

    Add a File to media folder with MediaService

    Hi there. I've been searching around for a solution and can't seem to find the problem.

    So what I want is pretty simple. I have a form and I want to upload a file to either a custom folder or a /media folder (i'm ok with both solutions, just want the file to be there).

    I call a Service to send an email and then add information to umbraco. At the moment I have the following:

        public void InsertItem(ApplicationsModelView item)
        {
            var groups = _contentService.GetRootContent().Where(x => x.ContentType.Alias == "home");
            var group = groups.First(x => x.Name == "Teste Folder");
    
            var applications = _contentService.GetDescendants(group.Id)
                            .Where(s => s.ContentType.Alias.Equals("applications", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
    
            var content = _contentService.CreateContent(Guid.NewGuid().ToString(), applications.Id, "applicationsItem");
    
            // Umbraco File Host
            byte[] tempByte = Convert.FromBase64String(item.Curriculum);
    
            //var imagesFolder = _mediaService.GetRootMedia().FirstOrDefault(m => m.Name.InvariantEquals("CVS")); --> this is optional - just wanted a better organization
    
            var mediaFile = _mediaServiceContext.CreateMedia(item.cvExtension, -1, Constants.Conventions.MediaTypes.File);
            Stream fileStream = new MemoryStream(tempByte);
    
            var fileName = "item.FullName + "-" + Path.GetFileNameWithoutExtension(item.cvExtension);
            // var filePath = "/Umbraco/CVS/" + fileName + Path.GetExtension(item.cvExtension); --> testing if I could add the path for the file...ignore this!
            mediaFile.SetValue("curriculum", fileName, fileStream);
            _mediaService.Save(mediaFile);
            _mediaServiceContext.Save(mediaFile);
    
            content.SetValue("fullName", item.FullName);
            //content.SetValue("curriculum", filePath);
    
            _contentService.SaveAndPublishWithStatus(content);
        }
    

    In order to give more insight, I have an item with the Document Type under. All files are getting there correctly appart from the file ("Curriculum") enter image description here

    From what I've understand the File "container" is created but the file isn't stored to the umbraco media folder. I don't understand what is wrong and how I should go about it.

    This is what happens (ignore the names, what matters is the upload file, type and size). enter image description here

    and this is what is expected enter image description here

    An important note...I can save directly on the "/media" folder and save the Url on the "application" item.

    What am I doing wrong?

    Thanks in advance

Please Sign in or register to post replies

Write your reply to:

Draft