Archive for August, 2010
Validate file size prior upload
If I ask you what is the most complex thing in HTML, what would you reply?
For me, it would be the file upload (or maybe character encoding, but this is not the topic! 😛 ).
One of the problem around the file upload functionality is that there is, no matter what, a file size limit set on the server. The limit could be 100Kb such as 100Mb depending on the configuration of the server.
But what happens if the user tries to upload a file bigger than the limit?
It’s simple, the file is going to be uploaded on the server until the limit is reached. Once it happens, the server returns an error message to the client.
Well, this is not ideal!
Indeed, depending on the connection speed of the user, the error message could be displayed a few minutes later. And you know how impatient users are! 😉
So obviously, the solution would be to validate the file size before the upload starts.
To do that, we can simply use the fileSize
Javascript function against the upload field.
But, surprising enough, this function doesn’t work on Internet Explorer! 👿
And the only solution I found to get the file size with IE was using ActiveX:
var oas = new ActiveXObject("Scripting.FileSystemObject"); var e = oas.getFile(document.forms[0].file.value); var size = e.size;
Finally, here is the whole Javascript function to validate the file size:
function validateFileSize(file, maxSize) { if (navigator.appName=="Microsoft Internet Explorer") { if (file.value) { var oas = new ActiveXObject("Scripting.FileSystemObject"); var e = oas.getFile(file.value); var size = e.size; } } else { if (file.files[0]!=undefined) { var size = file.files[0].fileSize; } } if (size!=undefined && size > maxSize) return false; return true; }
with file
the file input field to validate and maxSize
the maximum size in bits.
For example, you can call this function as follow:
validateImageSize(document.forms[0].file, 500000)
I successfully tested this code on:
- Google Chrome 6;
- Firefox 3.6;
- Internet Explorer 6;
- Internet Explorer 7.
Relation between TortoiseSVN and locale
The other day, I got stuck on a problem with TortoiseSVN for about half a day! 😯
Yes, I know, it is a pretty long time to get a simple subversion client working…
The error I was continuously getting was:
Network connection closed unexpectedly
Nothing else! Nothing in the log files on both the client and the server. 🙁
The funny thing is that it was working perfectly fine using a command line client or even Eclipse Subversive.
After a (too) long time of investigation, I noticed that I was getting the following warnings when I was running the following command line directly on the server:
$ svn list svn+ssh://smoreau@localhost/data/svn/ svn: warning: cannot set LC_CTYPE locale svn: warning: environment variable LANG is en_US.UTF-8 svn: warning: please check that your locale name is correct smoreau@localhost's password: svnserve: warning: cannot set LC_CTYPE locale svnserve: warning: environment variable LANG is en_US.UTF-8 svnserve: warning: please check that your locale name is correct branches/ tags/ trunk/
Actually, any svn
command was triggering the warning messages:
$ svn info svn: warning: cannot set LC_CTYPE locale svn: warning: environment variable LANG is en_US.UTF-8 svn: warning: please check that your locale name is correct
Could it possibly be related with my TortoiseSVN problem?
Because I didn’t have any other idea, I decided to give it a go. 😉
It appears that these warning messages came from a configuration problem around the locale package on the server. Indeed, look what we get if we run the locale
command:
$ locale -a locale: Cannot set LC_CTYPE to default locale: No such file or directory locale: Cannot set LC_MESSAGES to default locale: No such file or directory locale: Cannot set LC_COLLATE to default locale: No such file or directory C POSIX
After some more investigation, it looks like this problem appeared when I upgraded the version of the locale package via APT. 😕
Once again, I asked my friend Google to help me out. It suggested me the following solutions:
- Run
locale-gen en_US.UTF-8
– Didn’t work - Run
update-locale LANG=en_US.UTF-8
– Didn’t work - Run
dpkg-reconfigure locales
– WORKED 😀
In conclusion, I wasn’t able to use TortoiseSVN because of a configuration problem on the locale package after an upgrade… Does it make sense? 🙄