Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 403 posts 1520 karma points
    Jan 21, 2015 @ 17:32
    Paul de Quant
    0

    Uploading a file to the Media Library via Code

    Hello,

    I'm wondering if one of you talented devs is able to answer my question.

    Can someone tell me how you upload a file to the media library, I've looked at the MediaService and while you can create Media, I can't see how you upload a file to go with the MediaType.

    I've got a file uploader on the frontend of my website and I need to upload and add custom information like Title, Description, Category to the file.

    Any examples would be greatly appreaciated

    Many Thanks

    Paul

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Jan 22, 2015 @ 09:40
    Richard Soeteman
    101

    Hi Paul,

    The Mediaservice is the way to go. Use a FileStream to read the file and use .SetValue method to store the file

    using (var fs = System.IO.File.OpenRead("/myfile"))
                                        {
                                            media.SetValue(prop.Alias, "filename", fs);
                                        }
    

    When you save the media it should have the file stored.

    Hope this helps,

    Richard

  • Paul de Quant 403 posts 1520 karma points
    Jan 22, 2015 @ 09:45
    Paul de Quant
    0

    Thanks Richard, I'll give it a try.

  • Paul de Quant 403 posts 1520 karma points
    Jan 22, 2015 @ 12:43
    Paul de Quant
    0

    Just to confirm Richards suggestion worked a treat. Many Thanks

    Here's my code if anyone needs it - I used it in a custom workflow for contour.

                IMediaService ms = ApplicationContext.Current.Services.MediaService;
                string path = HttpContext.Current.Server.MapPath(selectedFile);
    
                using (var fs = System.IO.File.OpenRead(path))
                {
                    var mediaFile = ms.CreateMedia(selectedFileName, 1357, "File");
    
                    string originalFileName = selectedFile.Split('/').Last();
    
                    mediaFile.SetValue("umbracoFile", originalFileName, fs);
    
                    mediaFile.SetValue("title", selectedFileName);
                    mediaFile.SetValue("description", selectedFileDesc);
    
                    ms.Save(mediaFile);
    
                }
    
  • Abdul Rahim 21 posts 83 karma points
    Apr 06, 2015 @ 13:45
    Abdul Rahim
    0

    Hi Paul,

    Thanks for this post...

    I got an error when I using the above code in my app.

    IMediaService ms = Services.MediaService;
     string path_ = Server.MapPath("~/Assets/rahim.jpg"); 
    using (var fs = System.IO.File.OpenRead(path_)) 
    {
     var mediaFile = ms.CreateMedia("rahim", uQuery.GetMediaByName(ParentMediaName).FirstOrDefault().Id, "Image",0); 
    mediaFile.SetValue("umbracoFile", fs);
     ms.Save(mediaFile); 
    }

     

    this is my error

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

    on this line

     mediaFile.SetValue("umbracoFile", fs);

     

    please help me

  • Paul de Quant 403 posts 1520 karma points
    Apr 07, 2015 @ 09:25
    Paul de Quant
    0

    Hi Abdul,

    The only difference I noticed in the code was that in mine I had:-

    mediaFile.SetValue("umbracoFile", originalFileName, fs);

    where as in yours it was:-

    mediaFile.SetValue("umbracoFile", fs);

    Try setting the new filename, when you're saving the file.

    Hope this helps.

    Cheers

    Paul

  • Abdul Rahim 21 posts 83 karma points
    Apr 07, 2015 @ 11:34
    Abdul Rahim
    0

    Hi paul ,

    I was tried that code. But it shows compilation error.. I dont know what is the problem..

    I need to resize image with ImageResizer and Upload it into media library

    Now I am directly upload Image file from file uploader to media library.

    I am create a new folder inside media folder using media ID.

     public string Createmedia_OnMediaFolder(HttpPostedFileBase _file)
            {
                if (_file != null)
                {
                    //Declare a new dictionary to store the parameters for the image versions.
                    var versions = new Dictionary<string, string>();               
    
                    //Define the versions to generate
                    versions.Add("_small", "maxwidth=600&maxheight=600&format=jpg");
    
    
                    //Generate each version
                    foreach (var suffix in versions.Keys)
                    {
                        ///Create a unique file name for each files to be uploaded into media library
                        string File_Name = DateTime.Now.ToString("dd MMM yyyy");
                        string Time = DateTime.Now.ToLongTimeString();
                        File_Name = File_Name + " " + Time;
                        File_Name = File_Name.Replace(":", "_");
                        File_Name = File_Name.Replace(" ", "_");
    
                        Media newMedia;
    
                        umbraco.BusinessLogic.User admUser = umbraco.BusinessLogic.User.GetUser(0);
                        ParentMediaName = "Review";
                        int Parent_Id = uQuery.GetMediaByName(ParentMediaName).FirstOrDefault().Id;
                        newMedia = Media.MakeNew(File_Name, MediaType.GetByAlias("Image"), admUser, Parent_Id);
    
                        string newName = File_Name; // This is calcualted earlier when file is uploaded
                        string newFolder = Server.MapPath("/media") + "\\" + newMedia.Id;
                        string newFile = newFolder + "\\" + newName;
    
                        ///
                        ///Create folder for Image in media folder using MediaID.
                        ///
                        ///
                        Directory.CreateDirectory(newFolder);
    
                        _file.InputStream.Seek(0, SeekOrigin.Begin);
                        //Let the image builder add the correct extension based on the output file type
                        ImageBuilder.Current.Build(
                            new ImageJob(
                                _file.InputStream,
                                newFile,
                                new Instructions(versions[suffix]),
                                false,
                                true));                    
    
    
                        FileInfo info = new FileInfo(newFile+".jpg");
    
    
                        ///
                        ///Create Image inside the folder to be created using media ID.
                        ///
                        newMedia.getProperty("umbracoFile").Value = newFile+".jpg";
                        newMedia.getProperty("umbracoBytes").Value = info.Length;
                        newMedia.getProperty("umbracoExtension").Value = "jpg";
                        int created_media_Id = newMedia.Id;
                        newMedia.Save();
                        return created_media_Id.ToString();
                    }
    
                }
                return "";
    
            }

    Now it is working perfectly I want..

    Thanks for your response.

     

  • Abdul Rahim 21 posts 83 karma points
    Apr 07, 2015 @ 11:48
    Abdul Rahim
    0

    Hi paul.

    Now I'm working on umbraco 7.2 forms.

    I dont  know how to apply styles for forms. I need to apply styles for each controls. I have more than one forms with diffrerent styles .

    I googled lot . But I didn't get a correct solution...

    Please help me.

  • Paul de Quant 403 posts 1520 karma points
    Apr 07, 2015 @ 12:22
    Paul de Quant
    0

    Hi Abdul,

    You can alter the styles, by creating your form in the back office and clicking on the Actions button in the top right hand corner and select the Styling option. From here you can disable the built in stylesheet and use your own.

    Alternatively, if that doesn't work you can take a look in the following folder:-

    /Views/Partials/Forms/Fieldtypes

    In here, you'll find the templates for each form element.

    Hope this helps.

    Cheers

    Paul

  • Abdul Rahim 21 posts 83 karma points
    Apr 08, 2015 @ 06:42
    Abdul Rahim
    0

    Thanks buddy

  • Dallas 133 posts 405 karma points
    Jun 11, 2016 @ 00:51
    Dallas
    0

    Addressing the compilation error Abdul was having when passing in a FileStream - you need to add a using statement for Umbraco.Core.Models to get the SetValue() overload that takes a FileStream.

    using Umbraco.Core.Models;
    

    Dallas

Please Sign in or register to post replies

Write your reply to:

Draft