Copied to clipboard

Flag this post as spam?

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


  • pbl_dk 150 posts 551 karma points
    Mar 23, 2018 @ 15:20
    pbl_dk
    0

    Media service download image from web

    Hi there forum.

    I am trying to download and create media images with Media Services. It's a really simple script. But the problem is that umbraco cant see a file of the data I am getting. I have a legit image from the stream. But umbraco returns:

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

    My code is:

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://img.youtube.com/vi/q7Uv_e2fkvo/hqdefault.jpg");
       HttpWebResponse response = (HttpWebResponse)req.GetResponse();
       Stream stream = response.GetResponseStream();
       Image img = Image.FromStream(stream); 
    
    
       var media = Services.MediaService.CreateMedia("youtubepreview", 2634, "Image");
       media.SetValue("umbracoFile", img);
       Services.MediaService.Save(media);
       stream.Close();
    

    Does anyone know why Umbraco cant see the file?

    //Cheers Peter

  • Alessandro Calzavara 32 posts 144 karma points c-trib
    Mar 23, 2018 @ 16:43
    Alessandro Calzavara
    1

    You are using the setvalue that sets the path of the file, but you have the actual file at hand. try with this code:

    var media = Services.MediaService.CreateMedia("youtubepreview", 2634, "Image");
    media.SetValue("umbracoFile", "hqdefault.jpg", stream);
    Services.MediaService.Save(media);
    stream.Close();
    
  • pbl_dk 150 posts 551 karma points
    Mar 23, 2018 @ 17:37
    pbl_dk
    0

    Hi Alessandro, thanks for helping me out. It's not really working. Now I get an error:

    Parameter is not valid.
    
  • pbl_dk 150 posts 551 karma points
    Mar 23, 2018 @ 18:18
    pbl_dk
    101

    Hi there. Thanks for putting me on the right track. I found another way to fetch the image. It's working now. :-)

     var mediaService = Services.MediaService;
       using (WebClient client = new WebClient())
       {
        Stream s = client.OpenRead("http://img.youtube.com/vi/q7Uv_e2fkvo/hqdefault.jpg");
        using (MemoryStream ms = new MemoryStream())
        {
         s.CopyTo(ms);
         var mediaImage = mediaService.CreateMedia("hqdefault", 2634, "Image");
         mediaImage.SetValue("umbracoFile", "hqdefault.jpg", ms);
         mediaService.Save(mediaImage);
        }
       }
    
Please Sign in or register to post replies

Write your reply to:

Draft