Copied to clipboard

Flag this post as spam?

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


  • Andreas Emtinger 23 posts 185 karma points
    Nov 17, 2016 @ 11:24
    Andreas Emtinger
    0

    Upload local files to Media programmatically

    Hi

    I cant find any working solution to upload local files to Umbraco programmatically.

    Tried this:

    uFile = mediaService.CreateMedia(fileName, parentId, mediaType);
    
    var streamFile = new FileStream(localFile, FileMode.Open);
    uFile.SetValue(Constants.Conventions.Media.File, streamFile);
    streamFile.Close();
    
    mediaService.Save(uFile);
    

    Error on uFile.SetValue():

    An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll

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

    Local file exists.

    if (File.Exists(localFile) == false) return;
    

    Any help?

  • Andreas Emtinger 23 posts 185 karma points
    Nov 17, 2016 @ 18:51
    Andreas Emtinger
    100

    Finally found the answer:

    Add:

    using Umbraco.Core.Models;
    

    Now we have more options to SetValue()

    uFile.SetValue(Constants.Conventions.Media.File, fileName, streamFile);
    

    Yey!

  • Andreas Emtinger 23 posts 185 karma points
    Nov 18, 2016 @ 14:36
    Andreas Emtinger
    0

    But, that didn't work.

    The files are listed in BackOffice, but they are not physical on the server (my machine).

    I am running a Console Application in another project, to create pages, upload files and set content.

    What more should I do? Copy files to Web project Media-folder? Doesn't feel like the best practice here.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies