By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

File size...

P: n/a
I have developed an upload tool, that allows users to upload files (funny
that). The asp form object has a 'SizeLimit' property that can be set, to
prevent files being uploaded, that are too large. Is there a way in
Javascript, to interrogate the file size before the form is posted, so that
the validation can be done before trying to post the form.

The only information I can find, on file size, relates to images. But any
file can be uploaded through this tool, so I also need to determine the size
of other binary and text files.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ruskin wrote:
I have developed an upload tool, that allows users to upload files (funny
that). The asp form object has a 'SizeLimit' property that can be set, to
prevent files being uploaded, that are too large. Is there a way in
Javascript, to interrogate the file size before the form is posted, so
that the validation can be done before trying to post the form.

The only information I can find, on file size, relates to images. But any
file can be uploaded through this tool, so I also need to determine the
size of other binary and text files.


Hi,

As far as I know: No. You cannot.

Javascript is intentionally crippled when it come to local file, for obvious
securityreasons.

Maybe clientside VB-script can: VB is well known for such 'features'.
I am unsure.
Anyway, that would limit your your clients to browsers that support
VB-script, not a wise choice. :-(

Regards,
Erwin Moller
Jul 23 '05 #2

P: n/a
Erwin Moller wrote:
Javascript is intentionally crippled when it come to local file, for obvious
securityreasons.
No, the browser DOM is intentionally crippled when it comes to local files.
JavaScript itself has no way of accessing files at all.

This is more than just nitpicking. JavaScript itself can contain security
vulnerabilities, for example I vaguely remember some Array or String overflow
vulnerability in early versions of JavaScript, where a specially constructed
Array or String could result in code execution.

But the more importantly, the DOM that JavaScript (or VBScript or PerlScript or
AnyScript) accesses can contain security vulnerabilities, and that is where most
of the problems appear.
Maybe clientside VB-script can: VB is well known for such 'features'.
I am unsure.
There is no difference between the things that can be done in Internet Explorer
between JavaScript and VBScript (well, there are differences, but not in terms
of the security implications you are referring to). In other words, the security
vulnerabilities are in the DOM and the ActiveX components, not really in the
scripting language itself.

If you can instanciate a FileSystemObject in VBScript and manipulate local
files, you can instanciate a FileSystemObject in JavaScript and manipulate local
files.
Anyway, that would limit your your clients to browsers that support
VB-script, not a wise choice. :-(


The choice would be between a user agent that support an object with properties
and methods capable of accessing the local file system, and using a user agent
that does not.

However, a solution to his question has already been provided (at least in
Internet Explorer):

<form name="myForm">
<input type="file" name="myFile">
<script type="text/javascript">
if ((new Image()).fileSize) {
document.write(
'<input type="button"' +
' name="mySize" value="Show Size"' +
' onclick="showSize(this.form);">'
);
}
function showSize(f) {
if ((new Image()).fileSize) {
var img = new Image();
img.onload = imgOnload;
img.onerror = imgOnerror;
img.src = f.elements['myFile'].value;
}
}
function imgOnload() {
if (this.fileSize) {
var size = this.fileSize;
if (size > 1024) {
size = Math.round((size / 1024) * 100) / 100;
}
alert(
'The file is ' + size +
(this.fileSize > 1024 ? ' kilobytes' : ' bytes') +
' in size.'
);
}
}
function imgOnerror() {
alert('The file is not an image or did not load.');
}
</script>
</form>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.