Copied to clipboard

Flag this post as spam?

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


  • Pagggy 28 posts 71 karma points
    Nov 09, 2022 @ 13:37
    Pagggy
    0

    media uploaded using code is not setting correct property data causing un selectable when picking from media picker

    Hi, I am uploading media file using following code and media created correctly but it is setting "umbracoFile" as media link instead of json object with cropper. Correct Property data

    Wrong property data

                var isImage = file.Name.ContainsAny(new[] { ".jpg", ".jpeg", ".bmp", ".gif", ".png" });
            var mediaTypeAlias = isImage ? ImageMediaTypeAlias : FileMediaTypeAlias;
            var fileName = Path.GetFileName(file.FullName);
            long children = 0;
            var mapMediaChildren = ms.GetPagedChildren(parentId, 0, 1000, out children);
    
            var mapMedia = mapMediaChildren.FirstOrDefault(c => c.Name == (name ?? fileName)); // look for same file name
    
            if (mapMedia == null)
            {
                mapMedia = ms.CreateMediaWithIdentity(name ?? fileName, parentId, mediaTypeAlias,userId:393);
                var buffer = System.IO.File.ReadAllBytes(file.FullName);
                mapMedia.SetValue(Constants.Media.Bytes, file.Length);
                mapMedia.SetValue(Constants.Media.Extension, file.Extension.Replace(".",""));
    
                int newImgHeight = 0;
                int newImgWidth = 0;
                //Bitmap img = new Bitmap(file.FullName);
                using (Image img = Image.FromFile(file.FullName))
                {
                    newImgHeight = img.Height;
                    newImgWidth = img.Width;
                }
    
                mapMedia.SetValue(Constants.Media.Height, newImgHeight);
                mapMedia.SetValue(Constants.Media.Width, newImgWidth);
    
                using (var stream = new MemoryStream(buffer))
                {
                   // mapMedia.SetValue("umbracoFile", null); // Set the file item to null as when upload to same media it was throwing error Illegal characters in path. Ref: http://issues.umbraco.org/issue/U4-9904
                    mapMedia.SetValue(contentTypeBaseServiceProvider, "umbracoFile", file.Name, stream);
    
                }
                ms.Save(mapMedia);
            }
            return mapMedia;
    
  • 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