473,287 Members | 1,533 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,287 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 193953
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.