Copied to clipboard

Flag this post as spam?

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


  • Nik Wahlberg 639 posts 1237 karma points MVP
    Aug 11, 2009 @ 23:25
    Nik Wahlberg
    0

    Creating Media Programatically

    Hi all,

    I am on Umbraco version 4.0.1.2 and have the need to create content programatically. I am aware of, and have in the past, created nodes programatically so that is no the issue. The question I have is, how would I create a media item from an uploaded file that I collect in the front-end form (which would be an asp:file control).

    Thanks for your examples and input.

    -- Nik

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Aug 12, 2009 @ 00:41
    Aaron Powell
    100

    You'll need to use the MakeNew method on Media:

    var media = Media.MakeNew("My Media", new MediaType(1234), new User(0), 4321);

    The parameters are:

    • Name of Media
    • Media Type to create (Folder, Image, File are the OOB ones)
    • User creating the media (I've hard-coded it but you can get it from the current logged in Umbraco user)
    • Parent ID
  • Nik Wahlberg 639 posts 1237 karma points MVP
    Aug 12, 2009 @ 00:56
    Nik Wahlberg
    0

    Thanks slace. I remeber seeing this now.

    So, am I looking to put the path in the media.Image attribute then?

    Sorry, this should probably be obvious.

    Thanks,
    Nik

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Aug 12, 2009 @ 03:41
    Nik Wahlberg
    2

    I found a complete solution for this and thought it would make sense to share this in this post.

    The following snippet is a slightly modified version of one that Paul Sterling posted on the old forum (thanks Paul for sharing):

         protected Media UmbracoSave(FileUpload uploadControl)
    {
    string mediaPath = "";
    Media m = null;
    if (uploadControl.PostedFile != null)
    {
    if (uploadControl.PostedFile.FileName != "")
    {
    // Find filename
    _text = uploadControl.PostedFile.FileName;
    string filename;
    string _fullFilePath;

    filename = _text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower();

    // create the Media Node
    // TODO: get parent id for current category - as selected by user (see below)
    // - for now just stick these in the media root :: node = -1
    m = Media.MakeNew(
    filename, MediaType.GetByAlias("image"), User.GetUser(0), 1219);

    // Create a new folder in the /media folder with the name /media/propertyid
    string mediaRootPath = HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") as string; // get path from App_GlobalResources
    string storagePath = mediaRootPath + m.Id.ToString();
    System.IO.Directory.CreateDirectory(storagePath);
    _fullFilePath = storagePath + "\\" + filename;
    uploadControl.PostedFile.SaveAs(_fullFilePath);

    // Save extension
    string orgExt = ((string)_text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1));
    orgExt = orgExt.ToLower();
    string ext = orgExt.ToLower();
    try
    {
    m.getProperty("umbracoExtension").Value = ext;
    }
    catch { }

    // Save file size
    try
    {
    System.IO.FileInfo fi = new FileInfo(_fullFilePath);
    m.getProperty("umbracoBytes").Value = fi.Length.ToString();
    }
    catch { }

    // Check if image and then get sizes, make thumb and update database
    if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + ext + ",") > 0)
    {
    int fileWidth;
    int fileHeight;

    FileStream fs = new FileStream(_fullFilePath,
    FileMode.Open, FileAccess.Read, FileShare.Read);

    System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
    fileWidth = image.Width;
    fileHeight = image.Height;
    fs.Close();
    try
    {
    m.getProperty("umbracoWidth").Value = fileWidth.ToString();
    m.getProperty("umbracoHeight").Value = fileHeight.ToString();
    }
    catch { }

    // Generate thumbnails
    string fileNameThumb = _fullFilePath.Replace("." + orgExt, "_thumb");
    generateThumbnail(image, 100, fileWidth, fileHeight, _fullFilePath, ext, fileNameThumb + ".jpg");

    image.Dispose();
    }
    mediaPath = "/media/" + m.Id.ToString() + "/" + filename;

    m.getProperty("umbracoFile").Value = mediaPath;
    m.XmlGenerate(new XmlDocument());
    }
    }

    // return the media...
    return m;
    }

    protected void generateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string fullFilePath, string ext, string thumbnailFileName)
    {
    // Generate thumbnail
    float fx = (float)fileWidth / (float)maxWidthHeight;
    float fy = (float)fileHeight / (float)maxWidthHeight;
    // must fit in thumbnail size
    float f = Math.Max(fx, fy); //if (f < 1) f = 1;
    int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);

    // fixes for empty width or height
    if (widthTh == 0)
    widthTh = 1;
    if (heightTh == 0)
    heightTh = 1;

    // Create new image with best quality settings
    Bitmap bp = new Bitmap(widthTh, heightTh);
    Graphics g = Graphics.FromImage(bp);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;

    // Copy the old image to the new and resized
    Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
    g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

    // Copy metadata
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
    ImageCodecInfo codec = null;
    for (int i = 0; i < codecs.Length; i++)
    {
    if (codecs[i].MimeType.Equals("image/jpeg"))
    codec = codecs[i];
    }

    // Set compresion ratio to 90%
    EncoderParameters ep = new EncoderParameters();
    ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);

    // Save the new image
    bp.Save(thumbnailFileName, codec, ep);
    bp.Dispose();
    g.Dispose();

    }
  • John C Scott 473 posts 1183 karma points
    Oct 01, 2009 @ 10:12
    John C Scott
    0

    this is a really lovely bit of code and works perfectly for what i am trying to do in my development environment

    but when i move it to the live environment i get a permissions errror

    Access to the path '1102' is denied.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.UnauthorizedAccessException: Access to the path '1102' is denied. 

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 

    To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

     

    where 1102 is the name of the media node id it is trying to create

    it does not  manage to create the folder

    this is IIS7 on Win2k3 and  the Network Service has full control of the media folder

     

     

  • Laurence Gillian 600 posts 1219 karma points
    Oct 01, 2009 @ 12:29
    Laurence Gillian
    0

    When I changed line 41 to 

    <code>

     string mediaRootPath = "/media/"; // get path from App_GlobalResources

    </code>

    The File data (size, width, height, etc) are correctly stored in the Database, but again, the new media folder and file are not created.

    L

  • Arnold Visser 418 posts 778 karma points hq c-trib
    Oct 29, 2009 @ 14:57
    Arnold Visser
    0

    I've run into the same problem. Did you already found the solution?

  • Arnold Visser 418 posts 778 karma points hq c-trib
    Oct 29, 2009 @ 15:05
    Arnold Visser
    0

    aah!  the files where crearted in C:/media/  instead of the right path... let me find out why that happend!

  • Laurence Gillian 600 posts 1219 karma points
    Oct 29, 2009 @ 15:09
    Laurence Gillian
    0

    Have you created the additional key in the web.config file, under <appSettings>

    Should be something like...

    <add key="MediaFilePath" value="c:/inetpub/wwwroot/media/" />

    /L

  • Arnold Visser 418 posts 778 karma points hq c-trib
    Oct 29, 2009 @ 15:13
    Arnold Visser
    0

    when I chaned  string mediaRootPath = "/media/"; // get path from App_GlobalResources to:

    string mediaRootPath = "/inetpub/websites/clientname/www/media/"; // get path from App_GlobalResources

    (my full path for the website)  everything is OK. It seems like the appsettings gives the wrong value...

  • Jason N 55 posts 79 karma points
    Jun 29, 2012 @ 17:23
    Jason N
    0

    Thanks.  This post has been helpful to me.  I'm not sure what this business about getting the media root path from appSettings is.  Wouldn't a simple Server.MapPath("~/media") suffice?  Or am I missing something?

  • Stephen Balmer 18 posts 38 karma points
    Jul 13, 2012 @ 15:31
    Stephen Balmer
    0

     

     

  • Daniel Howell 21 posts 42 karma points
    Nov 14, 2012 @ 01:53
    Daniel Howell
    1

    Just thought I would post a reply here, since I found a much easier way to do this in 4.9 using code from the umbraco source (not sure if it was this easy in older versions):

    Just create a PostedMediaFile from your file stream, create an UmbracoImageMediaFactory (specifically if you are doing images - there is also one for other files), and call DoHandleMedia, passing in the current media item and PostedMediaFile you just created.

    PostedMediaFile postedMediaFile = new PostedMediaFile
    {
        FileName = YourFileName,
        DisplayName = YourDisplayName,
        ContentLength = StreamLength,
        InputStream = Stream,
        ReplaceExisting = true
    };
    UmbracoImageMediaFactory factory = (UmbracoImageMediaFactory)MediaFactory.GetMediaFactory(parentNodeId, postedMediaFile, new User(0));
    factory.DoHandleMedia(ucImage, postedMediaFile, new User(0));

    Too easy!

  • Luke Alderton 192 posts 509 karma points
    Oct 10, 2013 @ 17:51
    Luke Alderton
    0

    Nice one Daniel, worked great for me! :D

  • Divya 3 posts 23 karma points
    Apr 17, 2014 @ 06:54
    Divya
    0

    HI

    When i click the save button i got the following error

    'The SaveAs method is configured to require a rooted path, and the path '1233' is not rooted.'

    this 1233 is Make a New folder ..... how to rectify this error

    Kindly reply me

  • Divya 3 posts 23 karma points
    Apr 17, 2014 @ 07:36
    Divya
    0

    kindly reply

  • Matthew Wood 6 posts 26 karma points
    Mar 09, 2015 @ 16:04
    Matthew Wood
    1

    Just wanted to update this post with my method I am using in Umbraco 7.
    My front end involves a form that allows a user to choose an image to upload for their business linsting.
    The back end code takes the file puts it in the media folder.

    FRONT END
     
    @{
        var businessListing = new BusinessListing();
    }
     
    @using (Html.BeginUmbracoForm<BusinessDirectorySurfaceController>("CreateListing"))
    {
        <fieldset>
            @Html.AntiForgeryToken()
            <legend>Create New Listing</legend>
     
            @Html.ValidationSummary("businessListing", true)
     
            @Html.LabelFor(m => businessListing.CompanyName)
            @Html.TextBoxFor(m => businessListing.CompanyName)
            @Html.ValidationMessageFor(m => businessListing.CompanyName)
            <br />
     
            @Html.LabelFor(m => businessListing.Image)
            <input type="file" name="file" />
            @Html.ValidationMessageFor(m => businessListing.Image)
            <br />
            
     
            <button>Submit</button>
        </fieldset>
     
    }


    BACK END / CODE BEHIND
             
    public ActionResult CreateListing([Bind(Prefix = "businessListing")]BusinessListing businessListing)
            {
                string id;
                
                if (Request.Files.Count > 0)
                {
                    var file = Request.Files[0];
                    if (file != null && file.ContentLength > 0)
                    {
                        // TODO: dynamically get node Id for Business Listing Thumbnail
                        // Create the media item
                        var mediaImage = Services.MediaService.CreateMedia(businessListing.CompanyName, 1396, Constants.Conventions.MediaTypes.Image);
                        // Save it to create the media Id
                        Services.MediaService.Save(mediaImage);
                        // Upload the image file
                        var fileName = Path.GetFileName(file.FileName);
                        // Create the new media folder
                        if (!Directory.Exists(Server.MapPath("~/media/" + mediaImage.Id + "/"))) Directory.CreateDirectory(Server.MapPath("~/media/" + mediaImage.Id + "/"));
                        // Build the file path
                        var path = Path.Combine(Server.MapPath("~/media/" + mediaImage.Id + "/"), fileName);
                        // Save the file to the server
                        file.SaveAs(path);                    
                        // Point the Umbraco media object at this new file                                                       
                        mediaImage.SetValue("umbracoFile", path);
                        // Save the new filepath
                        Services.MediaService.Save(mediaImage);     
                    }
                }
    // The rest of your code
    }

Please Sign in or register to post replies

Write your reply to:

Draft