Copied to clipboard

Flag this post as spam?

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


  • Charly 5 posts 26 karma points
    Nov 29, 2011 @ 11:29
    Charly
    0

    Upload Form to Media Library

    Hi all,

    How would I go about creating a upload form on the front end to allow users to upload files to the media library?

    I have the following code:

    var fileName = "";
        if (IsPost) {
            var fileSavePath = "";
            var uploadedFile = Request.Files[0];
            fileName = Path.GetFileName(uploadedFile.FileName);
            fileSavePath = Server.MapPath("~/media/" +
              fileName);
            uploadedFile.SaveAs(fileSavePath);
        }

    which uploads files to the media folder, although the files arn't added to the Media Library on the backend. I assume I need to use the Media properties but I'm pretty much lost.

    Any help would be much appreciated !!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 29, 2011 @ 12:37
    Jeroen Breuer
    0

    Maybe have a look at this package: http://our.umbraco.org/projects/website-utilities/gecko-uploadify. It also has a usercontrol for frontend uploading.

    Jeroen

  • Charly 5 posts 26 karma points
    Nov 29, 2011 @ 16:58
    Charly
    1

    Thanks Jeroen, sadly the package didn't work, although I managed to solve my problem. For anyone with a similar problem here's my code:

     

    @using umbraco.MacroEngines;
    @using umbraco.cms.businesslogic.media;
    @using umbraco.BusinessLogic;

    @{
        Media UploadedMedia; 
        umbraco.BusinessLogic.User Me = umbraco.BusinessLogic.User.GetUser(0);
        var fileName = "";
        if (IsPost)
        {
            var fileSavePath = "";
            var uploadedFile = Request.Files[0];      
            fileName = Path.GetFileName(uploadedFile.FileName);
            fileSavePath = Server.MapPath("~/media/" + fileName);
            uploadedFile.SaveAs(fileSavePath);
            FileInfo info = new FileInfo(fileSavePath);
            string[] ext = fileName.Split('.');
            UploadedMedia = Media.MakeNew(fileName,MediaType.GetByAlias("File"),Me,1058);
            UploadedMedia.getProperty("umbracoFile").Value = "/media/" + fileName;
            UploadedMedia.getProperty("umbracoBytes").Value = info.Length;
            UploadedMedia.getProperty("umbracoExtension").Value = ext[1];
            UploadedMedia.Save(); 
           
        }
      }  
       
        @FileUpload.GetHtml(
            initialNumberOfFiles: 1,
            allowMoreFilesToBeAdded: false,
            includeFormTag: true,
            uploadText: "Upload")
        @if (IsPost)
        {
            File uploaded!

        }

     

  • Jonas Eriksson 930 posts 1825 karma points
    Dec 14, 2011 @ 12:57
    Jonas Eriksson
    0

    Thanks for this

    Just a note (stumbled on this myself just now) - if you're not using includeFormTag (for example within a bigger form), you'll need to add the attribute enctype="multipart/form-data" to your form tag to get the files.

Please Sign in or register to post replies

Write your reply to:

Draft