Copied to clipboard

Flag this post as spam?

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


  • Dee 118 posts 338 karma points
    Oct 22, 2019 @ 21:21
    Dee
    0

    Upload Image to media folder

    Hi,

    I'm using the following code, to download an image from specific URL and upload it to a media folder:

    public class ImageService
    {
        private readonly IMediaService _mediaService;
    
        public ImageService(IMediaService mediaService)
        {
            _mediaService = mediaService;
        }
    
        public int UploadMedia(string imageSrc)
        {
            IMedia newImage = null;
            try
            {
                var mediaSvc = _mediaService;
                var mediaExtension = Path.GetExtension(imageSrc);
                var imageName = Guid.NewGuid() + mediaExtension;
    
                var image = DownloadImageFromUrl(imageSrc);
    
                const string rootPath = "~\\media";
                var fileName =
                HttpContext.Current.Server.MapPath(Path.Combine(rootPath, imageName));
                image.Save(fileName);
    
                var parentFolderId = 5177;
                newImage = mediaSvc.CreateMedia(imageName, parentFolderId, "Image");
    
                var buffer = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));
    
                newImage.SetValue("umbracoFile", new MemoryStream(buffer));
                mediaSvc.Save(newImage);
    
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~\\media\\" + imageName)))
                {
                    System.IO.File.Delete(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));
                }
            }
            catch (Exception ex)
            {
                // log the exception
            }
            return newImage.Id;
        }
    
        private System.Drawing.Image DownloadImageFromUrl(string imageUrl)
        {
            System.Drawing.Image image = null;
            try
            {
                var webRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
                webRequest.AllowWriteStreamBuffering = true;
                webRequest.Timeout = 30000;
    
                var webResponse = webRequest.GetResponse();
                var stream = webResponse.GetResponseStream();
                image = System.Drawing.Image.FromStream(stream);
    
                webResponse.Close();
            }
            catch (Exception ex)
            {
                // log the exception
            }
            return image;
        }
    }
    

    downloading and saving in to the file system works. the image file can be opened and all good. It gets also uploaded to the subfolder with the ID in the media folder, but the uploaded image file is not really an image file, looks like a zombie.

    It doesn't have a thumbnail, no width, height, size, ... I also can't open the image. see the screenshot

    enter image description here

    any ideas?

    thanks

    dee

  • Dee 118 posts 338 karma points
    Oct 23, 2019 @ 12:53
    Dee
    0

    needed to use the extension SetValue-method of Umbraco.Core instead:

    newImage.SetValue(Current.Services.ContentTypeBaseServices, "umbracoFile", imageName, stream);
    
  • dave 4 posts 24 karma points
    Jun 10, 2020 @ 18:34
    dave
    0

    I am getting an error when trying exactly this solution.

    "The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetValue(string, object, string, string)' has some invalid arguments"
    

    What do I have to do to make sure I can use this extension method please?

  • Dee 118 posts 338 karma points
    Jun 10, 2020 @ 21:46
    Dee
    0

    Send your code pls

  • dave 4 posts 24 karma points
    Jun 10, 2020 @ 22:28
    dave
    0

    I posted the code and link to this thread on stack overflow as a question:

    https://stackoverflow.com/questions/62310126/umbraco-8-setvalue-has-some-invalid-arguments-saving-new-image-to-media-c-sha

    Thanks

  • Dee 118 posts 338 karma points
    Jun 11, 2020 @ 10:07
    Dee
    0

    it seems to me like you are not using the umbraco.core extension method setValue. If you click on the method name SetValue and press F12, you should be navigated to Umbraco.Core.ContentExtensions.SetValue method, if not, then something is wrong with your call.

    I wonder why you are not using Current.Services but directly the Services class.

    This is how it works for me:

        private static IMedia AddOrEditReferenceImage(MemberModel model, IMember member, int mediaId)
        {
            var rootMedia = Current.Services.MediaService.GetRootMedia().First();
    
            long totalrecords = 0;
            var children = Current.Services.MediaService.GetPagedChildren(rootMedia.Id, 0, 1000, out totalrecords);
    
            var folder = children.Where(x => x.Name.Equals(member.Name)).FirstOrDefault();
    
            if (folder == null)
            {
                folder = Current.Services.MediaService.CreateMedia(member.Name, rootMedia.Id, "Folder");
                Current.Services.MediaService.Save(folder);
            }
    
            var newImage = Current.Services.MediaService.CreateMedia(model.CompanyCaseStudies.AddedImage.FileName, folder.Id, "Image");
    
            newImage.SetValue(Current.Services.ContentTypeBaseServices, "umbracoFile", model.CompanyCaseStudies.AddedImage.FileName, model.CompanyCaseStudies.AddedImage.InputStream);
    
            Current.Services.MediaService.Save(newImage);
    
            if (mediaId != -1)
                Current.Services.MediaService.Delete(Current.Services.MediaService.GetById(mediaId));
    
            return newImage;
        }
    

    by using Umbraco.Core import. Hope this helps you

  • dave 4 posts 24 karma points
    Jun 11, 2020 @ 12:40
    dave
    0

    I have fixed this by changing the lines

    dynamic newImage = null; newImage = mediaSvc.CreateMedia(imageName, -1, "Image");

    to:

    var newImage = mediaSvc.CreateMedia(imageName, -1, "Image");

    So it must be the dynamic keyword that was causing the method to match to the wrong signature, not the extension method.

  • Dee 118 posts 338 karma points
    Jun 12, 2020 @ 10:21
    Dee
    0

    great!

Please Sign in or register to post replies

Write your reply to:

Draft