Copied to clipboard

Flag this post as spam?

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


  • athul 26 posts 117 karma points
    May 09, 2017 @ 08:02
    athul
    0

    Unable to create media item programatically

    My code to create an Umbraco media item is like this

    string fairLogopath = "/Images/FairImages/986185826.png";
        IMedia media = ApplicationContext.Current.Services.MediaService.CreateMedia("name", -1, Constants.Conventions.MediaTypes.Image);
        var mediaService = Services.MediaService;
        mediaService.Save(media); // called it before so it creates a media id
        FileImportWrapper file = new FileImportWrapper(IOHelper.MapPath(fairLogopath));
        media.SetValue(Constants.Conventions.Media.File, file);
        ApplicationContext.Current.Services.MediaService.Save(media);
    

    Everything looks okay to me, but this throws an error

    The process cannot access the file 'C:\Users\Documents\Studio\media\1115\986185826_thumb.png' because it is being used by another process.
    

    Can anyone please point out what I am doing wrong here?

  • Alex Skrypnyk 6161 posts 24131 karma points MVP 8x admin c-trib
    May 09, 2017 @ 09:53
    Alex Skrypnyk
    0

    Hi Athul

    Why did you call .Save method 2 times?

    /Alex

  • athul 26 posts 117 karma points
    May 10, 2017 @ 09:46
    athul
    0

    removed the second save and changed my code as Dave suggested, still I am getting same error :(

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 09, 2017 @ 09:57
    Dave Woestenborghs
    0

    Hi Athul,

    Can you try this code :

    string fairLogopath = "/Images/FairImages/986185826.png";
    
        var mediaService = Services.MediaService;
         IMedia media = mediaService.CreateMedia("name", -1, Constants.Conventions.MediaTypes.Image);    
        FileImportWrapper file = new FileImportWrapper(IOHelper.MapPath(fairLogopath));
        media.SetValue(Constants.Conventions.Media.File, file);
        mediaService.Save(media);
    

    Dave

  • athul 26 posts 117 karma points
    May 10, 2017 @ 09:42
    athul
    0

    Throwing same error

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 10, 2017 @ 09:48
    Dave Woestenborghs
    0

    Hi Athul,

    What is FileImportWrapper ? This doesn't seem to be a Umbraco object ? Is it something custom ?

    Dave

  • athul 26 posts 117 karma points
    May 10, 2017 @ 10:51
    athul
    0

    Yes, This is the complete code of it.

        public sealed class FileImportWrapper : HttpPostedFileBase
    {
        private FileInfo fileInfo;
    
        public FileImportWrapper(string filePath)
        {
            this.fileInfo = new FileInfo(filePath);
        }
    
        public override int ContentLength
        {
            get
            {
                return (int)this.fileInfo.Length;
            }
        }
    
        public override string ContentType
        {
            get
            {
                return MimeExtensionHelper.GetMimeType(this.fileInfo.Name);
            }
        }
    
        public override string FileName
        {
            get
            {
                return this.fileInfo.FullName;
            }
        }
    
        public override System.IO.Stream InputStream
        {
            get
            {
                return this.fileInfo.OpenRead();
            }
        }
    
        public static class MimeExtensionHelper
        {
            static object locker = new object();
            static object mimeMapping;
            static 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 });
            }
        }
    }
    

    If there is anyway to achieve this with out this helper. pls let me know

  • Craig Mayers 164 posts 508 karma points
    May 17, 2017 @ 15:01
    Craig Mayers
    0

    Hi athul,

    It will be your custom class "FileImportWrapper" that will be holding onto the file that you are trying to use.

    You can test out this theory quickly, by just commenting out the following line:

    FileImportWrapper file = new FileImportWrapper(IOHelper.MapPath(fairLogopath));
    

    Let me know how it goes.

    EDIT: Would also be worth showing your code for 'IOHelper.cs' so we can see what you are doing in this file.

    Regards

    Craig

  • Michele Di Maria 34 posts 239 karma points
    Aug 30, 2017 @ 15:40
    Michele Di Maria
    0

    Hi, I was using the class above until I had the need to "clean" the file once uploaded and I found out that the method above leaves the input file opened.

    To compensate we need to integrate the FileImportWrapper as below:

    First: we declare a private FileStream

    private FileStream fileStream;
    

    Then we initialize and return that object in the InputStream property:

        public override System.IO.Stream InputStream
        {
            get
            {
                fileStream = this.fileInfo.OpenRead();
                return fileStream;
            }
        }
    

    and we create a new property "Close" as below:

        public bool Close
        {
            get
            {
                if (fileStream != null)
                    fileStream.Close();
                return true;
            }
        }
    

    When using the wrapper, after saving the media item, you just:

    bool x = BinaryFile.Close;
    

    and the file will not be in use anymore. You can delete the source file just after the Close method.

    To summarize, below the complete class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Web;
    using System.Reflection;
    
    
    /// <summary>
    /// Class needed to save a binary file in Umbraco using the MediaService
    /// </summary>
    namespace BusinessObjects
    {
        public sealed class FileImportWrapper : HttpPostedFileBase
        {
            private FileInfo fileInfo;
            private FileStream fileStream;
    
            public FileImportWrapper(string filePath)
            {
                this.fileInfo = new FileInfo(filePath);
            }
    
            public bool Close
            {
                get
                {
                    if (fileStream != null)
                        fileStream.Close();
                    return true;
                }
            }
    
            public override int ContentLength
            {
                get
                {
                    return (int)this.fileInfo.Length;
                }
            }
    
            public override string ContentType
            {
                get
                {
                    return MimeExtensionHelper.GetMimeType(this.fileInfo.Name);
                }
            }
    
            public override string FileName
            {
                get
                {
                    return this.fileInfo.FullName;
                }
            }
    
            public override System.IO.Stream InputStream
            {
                get
                {
                    fileStream = this.fileInfo.OpenRead();
                    return fileStream;
                }
            }
    
            public static class MimeExtensionHelper
            {
                static object locker = new object();
                static object mimeMapping;
                static 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 });
                }
            }
        }
    }
    

    and here is how I use it:

                BusinessObjects.FileImportWrapper BinaryFile = new BusinessObjects.FileImportWrapper(SourceDocumentFile);
                MediaDocument.SetValue(Constants.Conventions.Media.File, BinaryFile);
                UmbracoMedia.Save(MediaDocument);
                bool x = BinaryFile.Close;
    

    Enjoy!

    Michele

Please Sign in or register to post replies

Write your reply to:

Draft