Copied to clipboard

Flag this post as spam?

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


  • antao 81 posts 371 karma points
    Apr 24, 2015 @ 16:01
    antao
    0

    MediaService not working to SetValue of 'umbracoFile'

    Hi all,

    I need to create some media nodes programmatically, and when using the MediaService like above:

    var mediaNode = mediaService.CreateMedia("integrationTestFile", 9384, "image");
    byte[] buffer = File.ReadAllBytes("C:\\1.jpg");
    MemoryStream memoryStream = new MemoryStream(buffer);
    mediaNode.SetValue("umbracoFile", memoryStream);
    

    It throws an exception on setting the umbracoFile value: "The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments"

    I see that I can pass an object in the first paramater. Am I missing something? I'm running 7.2.4 version.

    Thanks.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 24, 2015 @ 17:45
    Alex Skrypnyk
    100

    Hi Antao,

    We are using code like that:

    var fileInfo = new FileInfo(imagePath);

        IMedia media = null;
    
        if (fileInfo.Exists)
        {
            media = ApplicationContext.Current.Services.MediaService.CreateMedia(player.Photo, playersPhotoMediaFolder.Id, Constants.Conventions.MediaTypes.Image);
    
            var file = new FileImportWrapper(IOHelper.MapPath(imagePath));
    
            media.SetValue(Constants.Conventions.Media.File, file);
    
            ApplicationContext.Current.Services.MediaService.Save(media, 0, false);
            file.InputStream.Close();
            file.InputStream.Dispose();
        }
    

    And FileImportWrapper.cs :

    public class FileImportWrapper : HttpPostedFileBase { private readonly FileInfo fileInfo;

    public FileImportWrapper(string filePath)
    {
        fileInfo = new FileInfo(filePath);
    }
    public override int ContentLength
    {
        get
        {
            return (int)fileInfo.Length;
        }
    }
    
    public override string ContentType
    {
        get
        {
            return MimeExtensionHelper.GetMimeType(fileInfo.Name);
        }
    }
    
    public override string FileName
    {
        get
        {
            return fileInfo.FullName;
        }
    }
    
    public override Stream InputStream
    {
        get
        {
            return fileInfo.OpenRead();
        }
    }
    
    public static class MimeExtensionHelper
    {
        static readonly object locker = new object();
        static readonly object mimeMapping;
        static readonly MethodInfo getMimeMappingMethodInfo;
    
        static MimeExtensionHelper()
        {
            Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
            if (mimeMappingType == null)
                throw new SystemException("Couldnt find MimeMapping type");
            ConstructorInfo constructorInfo = mimeMappingType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
            if (constructorInfo == null)
                throw new SystemException("Couldnt find default constructor for MimeMapping");
            mimeMapping = constructorInfo.Invoke(null);
            if (mimeMapping == null)
                throw new SystemException("Couldnt find MimeMapping");
            getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
            if (getMimeMappingMethodInfo == null)
                throw new SystemException("Couldnt find GetMimeMapping method");
            if (getMimeMappingMethodInfo.ReturnType != typeof(string))
                throw new SystemException("GetMimeMapping method has invalid return type");
            if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
                throw new SystemException("GetMimeMapping method has invalid parameters");
        }
    
        public static string GetMimeType(string filename)
        {
            lock (locker)
                return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
        }
    }
    

    }

    Everything works fine, but not very fast ))

    Thanks, Alex

Please Sign in or register to post replies

Write your reply to:

Draft