1

Easiest way to implement the 1mb limitation on file upload. Do I use event handler to take care of this or javascript? I can have the event handler ready as I have messed with it before. It is better to handle it with javascript? Please suggest.

Edited: I got this far from a blog.

<script type="text/javascript">

var FSO = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
var input_File = document.aspnetForm.ctl00_PlaceHolderMain_ctl01_ctl02_InputFile.value;
var uploaded_file = FSO.getFile(input_File);
var file_size = uploaded_file.size;        //size in bytes
​​​​ </script>

4 Answers 4

1

Try this...

http://hi.baidu.com/ng19871221/blog/item/3224169862949f1c6e068cc9.html

private bool ValidFile(SPItemEventProperties properties)

{

     long validFileSize;

     long currentFileSize;

     if (properties.ListItem == null)

     {

             using (SPWeb web = properties.OpenWeb())

             {

                    if (!web.CurrentUser.IsSiteAdmin)

                   {



                          if (properties.ListTitle.ToLower() == "images")

                         {



                                 if (ConfigurationManager.AppSettings["FileSize"] != null)


                                 {

                                        validFileSize = long.Parse(ConfigurationManager.AppSettings["FileSize"].ToString());



                                 }

                                 else



                                {



                                       validFileSize = 1000000;//1MB



                                }



                               currentFileSize = long.Parse(properties.AfterProperties["vti_filesize"].ToString());



                               if (currentFileSize > validFileSize)



                              {


                                      return false;



                              }



                        }



                 }



          }

    }

    else if (properties.ListItem.ParentList.Title.ToLower() == "images")

    {

          if (!properties.ListItem.Web.CurrentUser.IsSiteAdmin) 

         {

                 if (ConfigurationManager.AppSettings["FileSize"] != null)


                 {

                        validFileSize = long.Parse(ConfigurationManager.AppSettings["FileSize"].ToString());



                 }

                 else

                 {

                        validFileSize = 1000000;//1MB

                 }

                 currentFileSize = properties.ListItem.File.TotalLength;

                 if (currentFileSize > validFileSize)

                {

                      return false;

                }

         }

    }

    return true;

} 
2
  • Thanks for the solution. I wish to utilize jquery or javascript type solution. @AWG: Actually, this link is good but I need to determine the file size before user can upload the file. jshidell.wordpress.com/2010/09/20/…
    – David Mike
    Commented Jul 8, 2011 at 21:56
  • I am accepting your answer but I am continue to pursue jQuery or Javascript type solution if possible.
    – David Mike
    Commented Jul 8, 2011 at 22:36
0

This question is subjective. I'm not sure there is a "best" way. General user interface guidlines I'm familiar with would suggest the sooner the user gets the feedback, the better. If you can do this with JavaScript without a server roundtrip, I would think that would be sooner. In that regard, JavaScript would be better.

The event handler might be a good catch-all in case something get's past the JavaScipt, though.

1
0

In short, you can't. At least not with JavaScript. It is impossible to know the size of the selected file BEFORE the user clicks on upload.

It is impossible because until the file gets sent to the server, the browser and sharepoint have no clue what's happening. And Javascript (i.e. the browser) can't "talk" to the machine it sits on and determine the file's size. And it can't do that because it would mean a major security threat.

Wouldn't be a good thing if you were to visit a site, and then some javascript starts reading files on your harddrive now would it? The only way to do this is by letting the file transfer start, so the server can see it's size, then have the transfer cancelled if the file size exceeds a certain number. So doing this serverside is the only way.

1
  • Those are really good points. I will then start with eventhandler solution. Thanks for you input.
    – David Mike
    Commented Jul 8, 2011 at 23:05
0

I would definately use an eventhandler (even a sandbox solution may fullfill your needs on it, if you have SP2010).

why not javascript? * clientside and can be circumvented * javascript can be turned off * when adding items via other ways(for example, powershell, webservices), the javascript is not executed. A eventhandler will always be executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.