Copied to clipboard

Flag this post as spam?

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


  • oskar 8 posts 29 karma points
    Jan 18, 2010 @ 14:08
    oskar
    1

    Upload datatype / member uploading image

    I would like to have members to be able to upload a photo of their selfs. I made a membertype with a upload datatype. In the back end, this works good. Editors can add / change / delete an image easily. But now I would like to use that same upload control in the frontend, available for members. I guess I need to use umbraco.editorControls.uploadField? But how can I use this control?

    Thanks in advance, Oskar

  • Andrew Blackmore 84 posts 127 karma points
    Mar 05, 2010 @ 19:55
    Andrew Blackmore
    0

    I'd like to see how to go about this as well...

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Mar 07, 2010 @ 02:38
    Aaron Powell
    0

    The uploadField DataType is really just a wrapper for the FileUpload control (well, actually HtmlInputFile, as it's a hold over from .NET 1.1 when FileUpload didn't exist).

    Really all you need to do is put your own FileUpload control on the form. To get it to save in the 'umbraco standard' folder path you need to upload to the /media/<property id>/ folder.

    It's just a matter of uploading a file using standard ASP.NET and then writing the path into the Value property of the property.

  • Hundebol 167 posts 314 karma points
    Mar 26, 2010 @ 13:39
    Hundebol
    0

    I'd like to see this as well...

    #slace

    It sound easy, but not for non-developers like myself - does someone have som sample code how to do this?

    best regards,
    hundebol

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Mar 26, 2010 @ 14:10
    Nik Wahlberg
    2

    Here is one way of doing this. 

    UserControlToUploadFile.ascx

    <asp:FileUpload ID="UserProfileImage" runat="server" />

    And here is the code-behind to process the request and create an image:

    // submit handler
    protected void submit_Click(object sender, EventArgs e)
    {
        int mediaId = SaveFile(UserProfileImage,{ID of parent in media tree}).Id;
    
        // here you can save the media ID to the member that you are editing by
        // simply setting the member property...something like:
        Member m = Member.GetCurrentMember();
        m.getProperty("memberProfilePic").Value = mediaId;
    }
    
    // Helper Method to upload and store your image in the media tree
    protected Media SaveFile(FileUpload uploadControl, int mediaParent)
    {
        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
                m = Media.MakeNew(
                    filename, MediaType.GetByAlias("image"), User.GetUser(0), mediaParent);
    
                // Create a new folder in the /media folder with the name /media/propertyid
                string mediaRootPath = HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") as string;
                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;
    }

    HTH,
    Nik

  • Ernst Utvik 123 posts 235 karma points
    Jul 30, 2010 @ 12:19
    Ernst Utvik
    0

    Thanks for the code, I was looking for something like this.

    Do I need to add any references to make this work?

  • Sascha Wolter 615 posts 1101 karma points
    Jul 30, 2010 @ 12:38
    Sascha Wolter
    0

    Hi Ernst,

    apart from the usual references to the Umbraco dll(s) for the Media class you do not need any additional references, the FileUpload control is standard ASP.Net.

    Cheers,

    Sascha

  • Rasmus Willumsgaard 6 posts 26 karma points
    Jan 10, 2012 @ 11:49
    Rasmus Willumsgaard
    0

     

    Sorry to bring up this old tread, I tried the code above but it’s giving me an 

    “Access to the path '1112' is denied.” Error (the path changes each time)

    But I have no problem setting other member values, like first and last name.

    It’s no problem uploading when logged in with admin in umbraco, and shouldn't this piece of code create the picture with the same privileges: 

    User.GetUser(0) or new umbraco.BusinessLogic.User(0) ??

    Any help would be appreciated 

     

  • Rasmus Willumsgaard 6 posts 26 karma points
    Feb 23, 2012 @ 09:39
Please Sign in or register to post replies

Write your reply to:

Draft