Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

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;

} 
share|improve this answer
    
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 Jul 8 '11 at 21:56
    
I am accepting your answer but I am continue to pursue jQuery or Javascript type solution if possible. –  David Mike Jul 8 '11 at 22:36
add comment

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.

share|improve this answer
    
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 Jul 8 '11 at 21:55
add comment

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.

share|improve this answer
    
Those are really good points. I will then start with eventhandler solution. Thanks for you input. –  David Mike Jul 8 '11 at 23:05
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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