473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Check file size with Javascript

Was wondering if anyone knew of way to validate an images file size using javascript - i.e keeping the upload to say 500k. I've seen scripts to validate the dimensions - but nothing for the actual file size.
Feb 23 '06 #1
6 194007
Niheel
2,460 Expert Mod 2GB
I usually just upload it to the server to check the file size. I don't think JS has the capabilities, could lead to security mis-uses if JS could access filesystem.

You could code something up if the all your user's allow ActiveX.
Feb 23 '06 #2
Yeah - i'll be verifying on the backend using php - but seems strange that js can tell what a file type is - what the dimensions are - but not the file size.
Feb 23 '06 #3
Hi
IT will help you.
Expand|Select|Wrap|Line Numbers
  1. function A()
  2. {
  3. var oas = new ActiveXObject("Scripting.FileSystemObject");
  4. var d = document.a.b.value;
  5. var e = oas.getFile(d);
  6. var f = e.size;
  7. alert(f + " bytes");
  8. }
  9.  
  10. </script>
  11. </head>
  12. <body>
  13. <form name="a">
  14. <input type="file" name="b">
  15. <input type="button" name="c" value="SIZE" onClick="A();">
  16. </form>
  17. </body>
  18. </html>
  19.  
Regards
Imran
Mar 12 '06 #4
function A()
{
var oas = new ActiveXObject("Scripting.FileSystemObject");
var d = document.a.b.value;
var e = oas.getFile(d);
var f = e.size;
alert(f + " bytes");
}

</script>
</head>
<body>
<form name="a">
<input type="file" name="b">
<input type="button" name="c" value="SIZE" onClick="A();">
</form>
</body>
</html>
Thanks Imran - so applying this to my forms validation should look like this right?

Expand|Select|Wrap|Line Numbers
  1. <script language=javascript>
  2. extArray = new Array(".jpg", ".jpeg",".gif");  //".png", , ".gif"
  3.  
  4. function callCancel()
  5. {
  6.     document.frmlisting.action="listing_list.php?cityid=<?=$cityid?>&c_id=<?=$c_id?>";
  7.     document.frmlisting.submit();
  8. }
  9.  
  10. function callSave()
  11. {
  12. var oas = new ActiveXObject("Scripting.FileSystemObject");
  13. var d = document.frmlisting.txtlistingimage.value;
  14. var e = oas.getFile(d);
  15. var f = e.size;
  16. alert(f + "500000");
  17. }
  18. {
  19.     if(!isCurrency(document.frmlisting.txtlistingprice.value)){
  20.         alert("Price: Incorrect data");
  21.         document.frmlisting.txtlistingprice.select();
  22.         return;
  23.     }
  24.     if(isBlank(document.frmlisting.txtlistingtitle.value)){
  25.         alert("Title is Required");
  26.         document.frmlisting.txtlistingtitle.focus();
  27.         return;
  28.     }
  29.     if(!isBlank(document.frmlisting.txtlistingimage.value)){
  30.         if(!isValidFile(document.frmlisting.txtlistingimage.value)){
  31.             alert("Selected file is not a vaild image type. \nPlease select "+ (extArray.join("  ").toUpperCase())+ " files. ");
  32.             document.frmlisting.txtlistingimage.select();
  33.             return;
  34.         }
  35.     }
  36.     if(isBlank(document.frmlisting.txtlistingemail.value)){
  37.         alert("Email is Required");
  38.         document.frmlisting.txtlistingemail.select();
  39.         return;
  40.  
  41.     }
  42.     if(!isEmail(document.frmlisting.txtlistingemail.value)){
  43.         alert("Email: Incorrect data");
  44.         document.frmlisting.txtlistingemail.select();
  45.         return;
  46.     }
  47.  
  48.     document.frmlisting.action="listingsubmit.php";
  49.     document.frmlisting.submit();
  50. }
  51. </script>

HTML
Would be

[HTML]<Input type="file" name="txtlistingimage" style="WIDTH: 275px; HEIGHT: 20px" size=39 maxlength=100>

<input type="button" class="btn_text" value="Preview" onclick="javascript:callSave();" style="border:solid-1px; color: #333333 ">[/HTML]
Mar 12 '06 #5
sashi
1,754 Expert 1GB
hi there,

am not sure if this code is useful.. give it a try.. good luck my fren..

http://www.quirksmode.org/js/filesize.html
Jun 27 '06 #6
Hello friend i came here to find check file size script. but i got activeXerror in first script and so now i create following script. if this help take it and try it.
1. this script load a image in Image Tag in Span Tag.
2. It check the file size.
3. if it is more than limit then remove image tag.
Expand|Select|Wrap|Line Numbers
  1. function imgLoad()
  2. {
  3. var strT = new String();
  4. document.getElementById("ShowImg").innerHTML= "";
  5. strT = document.ModReg.file1.value;
  6. if(strT != "")
  7. {
  8.     strT = "<img id='Sample' src='" + strT + "' width='100' height='100'></img>";
  9.     document.getElementById("ShowImg").innerHTML= strT;
  10.     if(!LimitedSize())
  11.     {
  12.        alert("File Size is more than 100 KB");
  13.        document.getElementById("ShowImg").innerHTML= "";
  14.     }
  15. }
  16. }
  17.  
  18. function LimitedSize()
  19. {
  20. var i;
  21. var y = document.images;
  22. for (i=0;i<y.length;i++)
  23. {
  24.     if((y[i].id) == 'Sample')
  25.     { 
  26.       if(y[i].fileSize > 102400)
  27.           return false;
  28.     }
  29. }
  30. return true;
  31. }
  32.  
//--------------HTML
Expand|Select|Wrap|Line Numbers
  1. <INPUT 
  2.       name=file1 type=file id="file1" onChange="imgLoad()" >
Sep 17 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Ken | last post by:
Is there a way to check the file size without uploading it? Check it at the source? If a large file (4m) is accidentally selected, it takes some time before the upload is complete. Is there...
2
by: tgundeck | last post by:
Does anyone know how I can check the file size of photos or any file type in JavaScript? I'm allowing users to store photos in my web page using the input type = file tag, but need to limit the...
1
by: Poppy | last post by:
I have written some code which uploads a file. Before I upload the file I would like to check on the file size. If its below a certain limit I want to upload it else present the user with an...
4
by: lawrence | last post by:
Using <INPUT type"file" runat="server"> to upload a file. When the file size is too large I get a "page cannot be display" error. Thats cool I'm down with that. But is there a way to check for...
6
by: nicholas | last post by:
when I upload a file, I can set in my code not to save it if the file size is too large. But then the user has allready been waiting a few minutes to upload his file, as this can only be...
3
by: puja | last post by:
hi all, In asp.net 2.0 there is a limit on file size upload of 4MB. I know this limit can be increased in web.config file. Is there any way to check the file size before upload ? if user is...
13
by: marfola | last post by:
I’m trying to find the file size using javascript, before it get uploaded into the server using the following Javascript (I don’t want to use ActiveX because of compatibility problem). function...
6
by: gyap88 | last post by:
hello, is there a command that returns the file size of a specific file as an integer
3
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.