Copied to clipboard

Flag this post as spam?

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


  • Scott Herbert 3 posts 23 karma points
    Aug 20, 2015 @ 16:24
    Scott Herbert
    0

    Umbraco Media API - Creating new items from existing files

    I'm trying to use the media API's to create an API control that takes a known file on the file system and creates an element withing the media library for it.

    My model is

        public class MediaUploadModel
    {
    
        public string Filename { get; set; }
        public string type { get; set; }
        public int node { get; set; }
    
        public string umbracoWidth { get; set; }
        public string umbracoHeigth { get; set; }
        public string umbracoExtension { get; set; }
        public string umbracoBytes { get; set; }
    
    }
    

    And my Controller is

            [HttpPost]
        public string UploadMediaToFolder(MediaUploadModel model) 
        {
    
                try
                {
                    Umbraco.Core.Models.IMedia newItem;
    
                    newItem = Services.MediaService.CreateMediaWithIdentity(model.Filename, model.node, model.type);
    
                    newItem.SetValue("umbracoFile", "/" + model.Filename);
                    newItem.SetValue("umbracoWidth", model.umbracoWidth);
                    newItem.SetValue("umbracoHeight", model.umbracoHeigth);
                    newItem.SetValue("umbracoExtension", model.umbracoExtension);
                    newItem.SetValue("umbracoBytes", model.umbracoBytes);
    
                    Services.MediaService.Save(newItem);
    
                    return newItem.Id.ToString();
                }
                catch (Exception ex)
                {
                    return "ERROR : " + ex.Message;
                }
    
        }
    

    The controller creates the new media item correctly but when the save method is called the setValue doesn't appear to be working correctly (I get values of -1 for the width,height and bytes the correct extension and a blank picture).

    I'm currently trying to import files stored in the root folder of the site, but aim to copy the files to the media folder then put then in correct folders within the media library.

    And all the POST requests will come from a non-web app (currently being simulated by a simple HTML form).

    Thanks in Advance.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 20, 2015 @ 20:50
    Marc Goodson
    0

    Hi

    I think the problem here is that the umbracoFile property is expecting a HttpPostedFile rather than a file path string.

    One way to work around this would be to create a new class that inherits HttpPostedFileBase

    that takes a file path as a constructor.

    Then use FileInfo class to generate information about the file to allow you to override

    • ContentLength
    • ContentType
    • FileName
    • InputStream

    using the FileInfo objects OpenRead() to create the InputStream from disc rather than a posted file

    Because your new class inherits HttpPostedFileBase, the SetValue("umbracoFile") should be happy to accept the new file

  • Scott Herbert 3 posts 23 karma points
    Aug 21, 2015 @ 11:29
    Scott Herbert
    0

    Thanks Marc,

    However I'm now getting the following exception thrown

    The method or operation is not implemented.
    

    With the following stack trace,

     at System.Web.HttpPostedFileBase.get_FileName() at Umbraco.Core.Models.ContentExtensions.SetValue(IContentBase content, String propertyTypeAlias, HttpPostedFileBase value) at Umbraco.Core.Models.ContentBase.SetPropertyValue(String propertyTypeAlias, HttpPostedFileBase value) at CallSite.Target(Closure , CallSite , Object , String , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2) at Umbraco.Core.Models.ContentBase.SetValue(String propertyTypeAlias, Object value) at Umbraco_DEV.Controllers.ContentPostbackApiController.UploadMediaToFolder(MediaUploadModel model) in c:\Users\sherbert\Downloads\Umbraco-DEV\Umbraco-DEV\Umbraco-DEV\Controllers\ContentPostbackApiController.cs:line 120
    

    On the SetValue method.

    My new class is

    public class myHttpPostFile : HttpPostedFileBase
    {
    
        new protected string FileName { get; set; }
        new protected int ContentLength { get; set; }
        new protected string ContentType { get; set; }
        new protected Stream InputStream { get; set; }
        public string FileExtension = "";
    
        public myHttpPostFile(string filePath)
        {
    
            this.FileName = filePath;
    
            FileInfo file = new FileInfo(filePath);
    
            this.ContentLength = (int)file.Length;
            this.InputStream = file.OpenRead();
            this.FileExtension = file.Name;
    
            this.ContentType = System.Web.MimeMapping.GetMimeMapping(filePath);
    
    
        }
    
    
    
    }
    

    Any thoughts? Thanks again.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 28, 2015 @ 10:09
    Marc Goodson
    0

    I think in the above you are setting the FileName to be the filePath, but the filePath, will probably be in the format /files/filename.doc and so therefore not be a valid FileName.

    Also you are using file.Name to set the FileExtension, however the Name will be the 'name' eg filename and not 'doc'

    I think I overrode the base properties of HttpPostedFileBase, eg

        public override int ContentLength
        {
            get
            {
                return (int)_fileInfo.Length;
            }
        }
    

    but that might not make a difference, just having the correct filename may make it work!

Please Sign in or register to post replies

Write your reply to:

Draft