size of fileupload
-
-
Hi i have a fileupload control in form, and now i want to check the size of file before upload it. i wrote this code but the value of fileSize = 0 all the time. what should i do. can anybody help me? int fileSize = fuSignature.PostedFile.ContentLength
ptvce wrote:
before upload
You code seems to be the server-side code and will be executed AFTER the file is uploaded. To check the file size before uploading the file, you've to use client side code i.e. JavaScript. But JavaScript does not allow accessing file system due to security reasons. Here[^] is an inelegant approach to do this if you are targeting only IE.
-
Hi i have a fileupload control in form, and now i want to check the size of file before upload it. i wrote this code but the value of fileSize = 0 all the time. what should i do. can anybody help me? int fileSize = fuSignature.PostedFile.ContentLength
That code doesn't work because ASP.NET is a server side technology. In other words, you have to transfer the posted file to the server before it can work out how big it is. There are things that you can do, but they generally involve compromises - one trick is to use a flash uploader control which can get access to the users hard disk to retrieve this information. Alternatively, if you know that your clients aren't going to be using Internet Explorer, you can use this function:
<script type='text/javascript'>
function showFileSize() {
var input, file;
if (typeof window.FileReader !== 'function') {
alert('The file API isn't supported by your browser');return;
}
input = document.getElementById('fileinput'); // Or whatever name your input control is...
if (!input) {
alert('You don't seem to have a file input control');
return;
}
if (!input.files) {
alert('This browser doesn't seem to support files.');
return;
}
if (!input.files[0]) {
alert('You havent picked a file');
return;
}file = input.files[0];
alert("The file is " + file.size + " bytes");
</script>If your clients have IE, you may have to use an ActiveX control. A final note - you really should have posted this in the ASP.NET forum.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier