+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to let registered (not cms) users upload images to a gallery

How to let registered (not cms) users upload images to a gallery

14 posts, 0 answered
  1. License Developer
    License Developer avatar
    31 posts
    Registered:
    18 Jun 2008
    12 Sep 2008
    Link to this post
    Hi,

    I'm looking for how I can allow registered visitors of my site (not CMS admins) to upload their own images into a specific photo gallery ("Waiting Approval") so that our CMS admins can review the images and decide if the admin wants to add the image into a photo gallery visible to our registered users.

    I'm assuming that I can reuse the same functionality that already exists in the CMS admin screens for uploading an image, make my own control, and drop that into a page so that registered visitors can also upload images, but I can't find where the code is that I need in the Sitefinity folders of my project.

    Sitefinity has a number of features that we'd eventually like to extend to our registered visitors as we build a community but it's been slow going trying to find out how to do so. Any help with this topic will probably also help a lot with us learning how to extend other modules to our registered visitors.

    Thanks!!!!

    Brett







  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    15 Sep 2008
    Link to this post
    Hello License Developer,

    You can learn more about Images&Documents API from here:
    http://www.sitefinity.com/help/developer-manual/libraries-api-overview.html

    We are using RadUpload control for file uploads, which comes for free with Sitefinity. You can find more about this control from here:
    http://demos.telerik.com/ASPNET/Prometheus/Upload/Examples/Overview/DefaultCS.aspx

    I hope you'll find this information helpful. Let us know if there is anything else we can do for you.

    Regards,
    Ivan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. License Developer
    License Developer avatar
    31 posts
    Registered:
    18 Jun 2008
    25 Sep 2008
    Link to this post
    Hi,

    When I browse to the API page I see the following:

    Service Unavailable



    fyi.
  4. Sonya
    Sonya avatar
    231 posts
    Registered:
    24 Sep 2012
    29 Sep 2008
    Link to this post
    Hi License Developer,

    Do you still experience the problem?

    Best wishes,
    Sonya
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. License Developer
    License Developer avatar
    31 posts
    Registered:
    18 Jun 2008
    30 Sep 2008
    Link to this post
    The page comes up fine now and I found how to repurpose the Telerik upload control, add the image to a predetermined library instead of the file system, and send the admin an email that there is a new image waiting for review / approval.

    If anyone would like to see the code that I'm using I'd be happy to post it here. Thanks for the help!

    Brett
  6. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    04 Oct 2008
    Link to this post
    I would love to see the code!  It sounds like a great feature and I would like to see it in action.

    If you wouldn't mind posting your code, that would be great.  If you have your own personal blog, you could also post it there as well.  Whatever is easiest for you.

    Gabe Sumner

  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    06 Oct 2008
    Link to this post
    Hello Brett,

    it is great to know that you managed to complete the code! As always, we will appreciate if you share it with the community.

    All the best,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. License Developer
    License Developer avatar
    31 posts
    Registered:
    18 Jun 2008
    06 Oct 2008
    Link to this post
    Hi Gabe / Georg,

    What I have is ugly and not production ready but it works. At this time I just needed to prove it could be done, and it works but it's not very elegant.

    There's also a bunch of commented out code that I don't need right now but I left in for future reference.

    For our purposes we also wanted the image moderator to get an email every time an image is uploaded to our "For Review" library. We also haven't skinned anything yet, I'm just using the existing skins for the upload control.

    Here's the code:
    UploadImages.ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UploadImages.ascx.cs" Inherits="CustomControls_UploadImages" %> 
     
    <%@ register tagprefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI" %> 
     
     
     
                <telerik:radprogressmanager id="Radprogressmanager1" runat="server" /> 
                <table width="754" border="0" cellpadding="0" cellspacing="0"
                    <tr> 
                        <td style="vertical-align: top; text-align: left;"
                            <span style="font-size:12px;">Allowed file extensions are ".jpg,.jpeg,.gif,.tif,.png"</span> 
                            <telerik:radupload 
                                id="RadUpload1" 
                                runat="server" 
                                Skin="Mac" 
                                initialfileinputscount="3" 
                                allowedfileextensions=".jpg,.jpeg,.gif,.png,.tif "  
                                MaxFileInputsCount="10" EnableEmbeddedSkins="True" /> 
                            <telerik:radprogressarea id="progressArea1" runat="server" Skin="Telerik"></telerik:radprogressarea> 
                            <asp:button id="buttonSubmit" runat="server" cssclass="RadUploadButton" onclick="buttonSubmit_Click" text="Submit" style="MARGIN-TOP: 6px" /> 
                        </td> 
                        <td style="vertical-align: top; text-align: left;"
                            <div class="module" style="MARGIN-TOP: 20px; FLOAT: right; OVERFLOW: auto; WIDTH: 280px; HEIGHT: auto;"
                                <asp:label id="labelNoResults" runat="server" visible="True">No uploaded files</asp:label> 
                                <asp:repeater id="repeaterValidResults" runat="server" visible="False"
                                    <headertemplate> 
                                        Uploaded valid files:<br /><br /> 
                                    </headertemplate> 
                                    <itemtemplate> 
                                        <%#DataBinder.Eval(Container.DataItem, "FileName")%> 
                                         
                                        ( 
                                        <%#DataBinder.Eval(Container.DataItem, "ContentLength").ToString() + " bytes"%> 
                                         )<br /><br /> 
                                    </itemtemplate> 
                                </asp:repeater> 
                            </div> 
                            <div class="module" style="MARGIN-TOP: 5px; FLOAT: right; OVERFLOW: auto; WIDTH: 280px; HEIGHT: auto;"
                                <asp:label id="labelNoInvalidResults" runat="server" visible="True">No invalid files</asp:label> 
                                <asp:repeater id="repeaterInvalidResults" runat="server" visible="False"
                                    <headertemplate> 
                                        It appears that some of the files you tried to upload are not valid files. Please make sure that they are image files and are under 2mb:<br /><br /> 
                                    </headertemplate> 
                                    <itemtemplate> 
                                        File: 
                                        <%#DataBinder.Eval(Container.DataItem, "FileName")%> 
                                        <br /> 
                                        File Size: 
                                        <%#DataBinder.Eval(Container.DataItem, "ContentLength").ToString() + " bytes"%> 
                                        <br /> 
                                        Mime-type: 
                                        <%#DataBinder.Eval(Container.DataItem, "ContentType").ToString()%> 
                                        <br /><br /> 
                                    </itemtemplate> 
                                </asp:repeater> 
                            </div> 
                        </td> 
                    </tr> 
                </table> 
                <br /><br /> 
                <b>Note:</b> The largest allowed combined file size for upload in this example is 100MB. This is specified by the 
                maxRequestLength="102400" set in Web.config file. If you attempt to upload files with total size greater that 100MB 
                you will get "Page Not Found" error. For more information about uploading large files visit the help article 
                <href="http://www.telerik.com/help/aspnet-ajax/upload_uploadinglargefiles.html" >Uploading Large Files</a> 
     
     
     



    UploadImages.ascx.cs
    using System; 
    using System.Collections; 
    using System.Configuration; 
    using System.Data; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Xml.Linq; 
     
    using System.ComponentModel; 
    using System.Drawing; 
    using System.IO; 
    using Telerik.Web.UI; 
    using Telerik.Libraries; 
     
    using System.Net.Mail; 
     
    public partial class CustomControls_UploadImages : System.Web.UI.UserControl 
        protected void Page_Load(object sender, EventArgs e) 
        { 
     
        } 
     
     
        protected void buttonSubmit_Click(object sender, System.EventArgs e) 
        { 
            BindValidResults(); 
            BindInvalidResults(); 
     
            //Remove this line if you do not want to delete the files 
            //in the location where they have been saved 
            //DeleteFiles(); 
        } 
        private void BindValidResults() 
        { 
            if (RadUpload1.UploadedFiles.Count > 0) 
            { 
                int imageIndex = 0; 
                foreach (UploadedFile validFile in RadUpload1.UploadedFiles) 
                { 
                     
                    //save to server 
                    //string targetFolder = Server.MapPath("~/StudentImages"); 
                    //validFile.SaveAs(Path.Combine(targetFolder, validFile.GetName()),  true); 
     
                    //save to library 
                    // create new instance of LibraryManager 
                    Telerik.Libraries.LibraryManager libraryManager = new Telerik.Libraries.LibraryManager(); 
                    // get library by specified Name 
                    Telerik.Libraries.ILibrary parentLibrary = libraryManager.GetLibrary("For Review"); 
                    // create an image content item 
                    Telerik.Cms.Engine.IContent testContent = libraryManager.CreateContent("image/jpeg"); //todo: update 
                    // specify parent library the image will belong to 
                    testContent.ParentID = parentLibrary.ID; 
     
                    //get the first uploaded file 
                    Telerik.Web.UI.UploadedFile firstFile = RadUpload1.UploadedFiles[imageIndex]; 
                    byte[] buffer = new byte[firstFile.InputStream.Length]; 
                    long position = 0; 
                    int currentByte = firstFile.InputStream.ReadByte(); 
                    while (currentByte != -1) 
                    { 
                       buffer[position++] = (byte)currentByte; 
                       currentByte = firstFile.InputStream.ReadByte(); 
                    } 
                    Telerik.Cms.Engine.IContent content = null
                    // upload the file 
                    try 
                    { 
                       content = libraryManager.UploadFile(buffer, 
                       firstFile.GetNameWithoutExtension(), 
                       firstFile.GetExtension(), 
                       firstFile.ContentType, 
                       firstFile.ContentLength, 
                       parentLibrary); 
                    } 
                    catch (ArgumentException ex) 
                    { 
                       Response.Write("There is an exception"); // :( ugly 
                    } 
                    // save the image as a content of the file 
                    testContent.Content = buffer; 
                
                    //email an alert to the admin 
                    MailMessage mail = new MailMessage(); 
     
     
                    //set the addresses 
                    mail.From = new MailAddress("NoReply@Site.com"); //Todo: take this out of code and make editable by cms users 
                    mail.To.Add("brettgarnier@lastpanda.com"); //Todo: take this out of code and make editable by cms users 
     
                    //set the content 
                    mail.Subject = "A new image was uploaded"//Todo: take this out of code and make editable by cms users 
                    mail.Body = "A new image was uploaded to the Library \"For Review\". Filename: " + validFile.GetName(); //Todo: take this out of code and make editable by cms users 
     
                    //send the message 
                      SmtpClient smtp = new SmtpClient("111.111.111.111");//Todo: take this out of code and make editable by cms users 
                    smtp.Send(mail); 
     
     
                    imageIndex++; 
                     
                } 
     
                labelNoResults.Visible = false
                repeaterValidResults.Visible = true
                repeaterValidResults.DataSource = RadUpload1.UploadedFiles; 
                repeaterValidResults.DataBind(); 
            } 
            else 
            { 
                labelNoResults.Visible = true
                repeaterValidResults.Visible = false
            } 
        } 
     
        private void BindInvalidResults() 
        { 
            if (RadUpload1.InvalidFiles.Count > 0) 
            { 
                labelNoInvalidResults.Visible = false
                repeaterInvalidResults.Visible = true
                repeaterInvalidResults.DataSource = RadUpload1.InvalidFiles; 
                repeaterInvalidResults.DataBind(); 
            } 
            else 
            { 
                labelNoInvalidResults.Visible = true
                repeaterInvalidResults.Visible = false
            } 
        } 
     
        /// <summary> 
        /// For the purpose of this demo we delete the files collection in the targetfolder 
        /// to prevent it from growing infinitely. 
        ///  
        /// OUT OF SERVICE 
        /// </summary> 
        private void DeleteFiles() 
        { 
            string targetFolder = Server.MapPath("~/StudentImages"); 
     
            DirectoryInfo targetDir = new DirectoryInfo(targetFolder); 
     
            try 
            { 
                foreach (FileInfo file in targetDir.GetFiles()) 
                { 
                    if ((file.Attributes & FileAttributes.ReadOnly) == 0) file.Delete(); 
                } 
            } 
            catch (IOException) 
            { 
            } 
        } 
     


    Like I said, it's not elegant but it's a working prototype. It should be plenty to work from if anyone else wants to allow visitors to upload files to a system folder or library. 


  9. Joe
    Joe avatar
    138 posts
    Registered:
    24 Sep 2012
    06 Oct 2008
    Link to this post
    Hello Brett,

    Thanks for the code sample. We appreciate your hard work and dedication. I am just following up to see if you are still having technical issues because I would like to close this ticket. If everything is all set, please reply to the ticket and close it.

    All the best,
    Joe
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  10. License Developer
    License Developer avatar
    31 posts
    Registered:
    18 Jun 2008
    06 Oct 2008
    Link to this post
    Insofar as my need to be able to give site members the ability to upload images I'm all set.  As other questions arise I'll open different tickets or post specific questions / comments.

    Thanks.

    Brett
  11. Adil Sardar
    Adil Sardar avatar
    1 posts
    Registered:
    05 Nov 2009
    14 Jun 2010
    Link to this post
    How could i assign permissions on uploaded files to be views only by selected users. Reply would be highly appreciated
  12. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    14 Jun 2010
    Link to this post
    Hi Adil Sardar,

    Permissions are supported only per library and not on files. Any permissions applied to the library, will be valid for all the files in it. This means that you should set the permissions on the library, to which the users are uploading their files. 

    Greetings,
    Georgi
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  13. Paul
    Paul avatar
    176 posts
    Registered:
    18 Nov 2008
    07 Oct 2010
    Link to this post

    Hi Georgi!

    You said that "Permissions are supported only per library and not on files. Any permissions applied to the library, will be valid for all the files in it.". Does this work in Sitefinity 3.7 SP4? Or it just implemented in Sitefinity 4 Beta2? How can I set permissions for library for special role?

    Thank you.

  14. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    07 Oct 2010
    Link to this post
    Hi Paul,

    Thank you for using our services.

    In Sitefinity 3.x versions permissions are applied on provider level. You cannot set permissions per library. You can however add additional library providers as described in this webinar and secure them separately.

    Regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
14 posts, 0 answered