Copied to clipboard

Flag this post as spam?

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


  • Jais Edelmann 5 posts 75 karma points
    Feb 24, 2021 @ 11:50
    Jais Edelmann
    0

    ContentService.SetValue file

    It's been quite a while since my last visit to the umbraco world. However I have a project now where I would like to create some content nodes via my Controller.

    I'm utilizing the ContentService which works fine for most fields. However for my property field where i have a Umbraco.UploadField things are not really working as expected.

    I've triede byte array etc. But not sucessfully. Please see controller snippet below.

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Web;

    namespace Website.Controllers { public class RequestTuneController : Umbraco.Web.Mvc.SurfaceController { public ActionResult Index() { return Content("hello world"); } [HttpPost] [Authorize] public ActionResult FormPost(Models.RequestTuneModel model) { var service = Services.ContentService; var content = service.Create("name", 1088, "TuningFile", -1);

            content.SetValue("ownerOfFile", Services.MemberService.GetById(Members.GetCurrentMemberId()).GetUdi().ToString());
    
            using(BinaryReader reader = new BinaryReader(model.FileFromPartner.InputStream))
            {
                byte[] array = reader.ReadBytes(model.FileFromPartner.ContentLength);
                content.SetValue("fileFromPartner", array);
            }
    
            service.SaveAndPublish(content);
    
            return RedirectToUmbracoPage(1234);
    
        }
    }
    

    }

  • Nik 1616 posts 7262 karma points MVP 7x c-trib
    Feb 24, 2021 @ 14:18
    Nik
    0

    Hi Jais,

    Unfortunately it's not that simple. Content items don't store the raw contetns of a file on them in an way. They store paths to the file in question, even is this is an "uploaded" one.

    This blog post from Paul Seal talks about uploading media items from the front end of your site:

    https://codeshare.co.uk/blog/how-to-upload-a-file-to-umbraco-from-a-front-end-form/

    I believe he's storing them in the media library, once you've done that you can have the property on your doc type as a media picker and set the value of that. Alternatively, you can push the file to the file system and then correctly set data on your content item. To find out what this value needs to look like, I advise manually creating a piece of content with an uploaded file, then examining the database to understand how that property value is represented in the umbracoPropertyData table :-)

    Cheers

    Nik

  • Jais Edelmann 5 posts 75 karma points
    Feb 26, 2021 @ 20:36
    Jais Edelmann
    0

    Hi Nik.

    When I use the interface for a content node that has a Umbraco.FileUpload I dont see that specific file in the media section? So I'm strugling abit to understand why when doing the file upload programaticly I have to place my files in the media content library ?

  • Jais Edelmann 5 posts 75 karma points
    Feb 28, 2021 @ 10:19
    Jais Edelmann
    0

    Here is what i ended up doing if anyone faces the same issue :) my requestTuneModel.File is just a HttpPostedFileWrapper

    var mediaFile = mediaService.CreateMedia(DateTime.Now.ToString("yyyy-MM-dd-") + contentName, 1155, "File",Members.GetCurrentMemberId());
                using (BinaryReader reader = new BinaryReader(requestTuneModel.File.InputStream))
                {
    
                    byte[] array = reader.ReadBytes(requestTuneModel.File.ContentLength);
                    using (Stream stream = new MemoryStream(array))
                    { 
                        mediaFile.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", requestTuneModel.File.FileName, stream);
                        var result = mediaService.Save(mediaFile);
                        content.SetValue("fileFromPartner", mediaFile.GetValue("umbracoFile"));
                    }
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft