1
votes

Im a bit new to Umbraco, and i have to say i like it a lot.

But now i'm stuck on something simple i think. I Created a protected page that is only visible to members on my website. Where the member is able to upload multiple files at once. This is al working like a charm. First i created the upload form for multiple images then i created the SurfaceController to handle the submit. Also working like a charm.

My ActionResult on my SurfaceController receives an IEnumerable<HttpPostedFileBase> called files which is good. I see all my images that i'm posting with my form. But here comes the problem.

While looping over my files I try to create a Media (Image type) using the MediaService.CreateMedia giving my filename and parentid and the mediaType (Image). But when i try to set the umbracoFile value on my just created media item i will get the following exception:

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

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

I hope someone can tell my what i'm doing wrong. Below is my code i'm using

[HttpPost]
    public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
    {
        bool success = false;
        //Get logged in member and look for the mediafolderID
        var member = Services.MemberService.GetByUsername(HttpContext.User.Identity.Name);
        var mediaFolderID = member.GetValue<int>("mediaFolderID");

        //Get mediafolder
        var mediaFolder = Services.MediaService.GetById(mediaFolderID);
        try
        {
            // Create a media item from each file uploaded
            foreach (var file in files)
            {
                var fileName = file.FileName; // Assumes no path information, just the file name
                var ext = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();

                if (!UmbracoConfig.For.UmbracoSettings().Content.DisallowedUploadFiles.Contains(ext))
                {

                    var mediaType = global::Umbraco.Core.Constants.Conventions.MediaTypes.File;
                    if (UmbracoConfig.For.UmbracoSettings().Content.ImageFileTypes.Contains(ext))
                    {
                        mediaType = global::Umbraco.Core.Constants.Conventions.MediaTypes.Image;
                    }
                    var f = Services.MediaService.CreateMedia(fileName, mediaFolderID, mediaType);

                    // Assumes the file.InputStream is a Stream - you may have to do some extra work here...
                    f.SetValue(global::Umbraco.Core.Constants.Conventions.Media.File,(Stream)file.InputStream); // Real magic happens here.
                    Services.MediaService.Save(f);
                }
            }
            success = true;
        }
        catch (Exception ex)
        {
            // On error show message
            ViewData["exceptionMessage"] = ex.Message;
            success = false;
        }

        // On success redirect to current page and show successmessage
        ViewData["success"] = success;
        if (success)
        {
            return RedirectToCurrentUmbracoPage();
        }

        return CurrentUmbracoPage();
    }
1
use Path.GetFileName() / Path.GetExtension() instead of the substring - Robert Baker

1 Answers

2
votes

Instead of f.SetValue(global::Umbraco.Core.Constants.Conventions.Media.File, (Stream)file.InputStream); you should just use the HttpPostedFileBase: f.SetValue(global::Umbraco.Core.Constants.Conventions.Media.File, file);

Some other notes:

  • Check that the file has a length and is not null: file != null && file.ContentLength > 0
  • You're not using your mediaFolder variable anywhere, can be removed.
  • Not sure why you'd need global::Umbraco.Core, consider adding using Umbraco.Core; and use Constants.Conventions.MediaTypes.Image etc.
  • Check that you really need to rely on DisallowedUploadFiles - I'm pretty sure that's checked during CreateMedia