locked
uploading images RRS feed

  • Question

  • Hi,

    I need to create a popup box to set an alert that only an image of particular size only can be uploaded in sharepoint 2010.  

    How can i achieve it?

    If possible,give some sample code.

    Thanks in advance for replies..

    Thursday, February 2, 2012 10:32 AM

Answers

  • You can achieve it using ItemAdding event handler. You can check the size of the image and if it is greater than the permissible size then simply cancel the event with an error message of your choice.

    Something like this should be helpful

    public override void ItemAdding(SPItemEventProperties properties)
            {
                try
                {
                       this.DisableEventFiring();
                       
                        currentFileSize = long.Parse(properties.AfterProperties["vti_filesize"].ToString());
                        if (currentFileSize > validFileSize)
                        {
                            properties.Status = SPEventReceiverStatus.CancelWithError;
                            properties.ErrorMessage = "Please upload images of less or equal to 5 MB";
                            properties.Cancel = true;
                        }
    
                        this.EnableEventFiring();
                }
                catch (Exception ex)
                {                
                    //Handle exception
                }
                finally
                {
                    this.EnableEventFiring();
                }
            }
    

     

    Hope it gives some idea.
    Thursday, February 2, 2012 6:47 PM

All replies

  • You can do this on Item Adding event handler.... or if you want to check things client side.. and you can use JQuery then you can wire up events on the upload controls may be 

      var request;

      request = $.ajax({

        type: "HEAD",

        url: $("#url").val(),

        success: function () {

          alert("Size is " + request.getResponseHeader("Content-Length"));

        }

      });

    Just a thought


    "T" | My blog updates | My Twitter | Our Products | Mail Me | LinkedIn | My Virtual Business Card

    A THOUGHT ABOUT MY WORKPLACE :)
    Thursday, February 2, 2012 11:07 AM
  • Hi,

    You cannot show javascript error messages or validation by using event handlers.


    Satyam MCITP, MCPD
    Thursday, February 2, 2012 11:39 AM
  • Hi,

    Thanks for the reply.

    I will try it out.Please do tell me if u are getting other ideas.

    Thursday, February 2, 2012 12:06 PM
  • Hi,

    I have adoubt.

    How can i bind this code with visual studio?

    Thursday, February 2, 2012 12:10 PM
  • Hmmm you create a js file to wire up JQuery events on teh browse button control... its not so straight forward thought....

     

    2nd thing... cant you use the SHarePoint default functionality to limit the file upload size?


    "T" | My blog updates | My Twitter | Our Products | Mail Me | LinkedIn | My Virtual Business Card

    A THOUGHT ABOUT MY WORKPLACE :)
    Thursday, February 2, 2012 12:13 PM
  • When you mention size, do you mean for example 200x200 px or do you mean max 10MB?

    Regards


    Thomas Balkeståhl - Technical Specialist - SharePoint - http://blksthl.wordpress.com
    Thursday, February 2, 2012 12:17 PM
  • I would insist to do that in item adding event
    Thursday, February 2, 2012 1:33 PM
  • You can achieve it using ItemAdding event handler. You can check the size of the image and if it is greater than the permissible size then simply cancel the event with an error message of your choice.

    Something like this should be helpful

    public override void ItemAdding(SPItemEventProperties properties)
            {
                try
                {
                       this.DisableEventFiring();
                       
                        currentFileSize = long.Parse(properties.AfterProperties["vti_filesize"].ToString());
                        if (currentFileSize > validFileSize)
                        {
                            properties.Status = SPEventReceiverStatus.CancelWithError;
                            properties.ErrorMessage = "Please upload images of less or equal to 5 MB";
                            properties.Cancel = true;
                        }
    
                        this.EnableEventFiring();
                }
                catch (Exception ex)
                {                
                    //Handle exception
                }
                finally
                {
                    this.EnableEventFiring();
                }
            }
    

     

    Hope it gives some idea.
    Thursday, February 2, 2012 6:47 PM
  • Hi,

    Thanks a lot for the sample code...

    I'm gonna try it out.

    Friday, February 3, 2012 4:14 AM
  • Hi guys,

    I got the requirement.

    Thanks a lot for all your replies...

    Friday, February 3, 2012 9:01 AM