473,396 Members | 2,021 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,396 software developers and data experts.

Problem with check file size with Javascript

13
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).

Expand|Select|Wrap|Line Numbers
  1. function imageSize (pathField, previewName)
  2. {       
  3.         var str = '';
  4.         var path = pathField.value;
  5.         document[previewName].src = 'file://'+path.replace(/\\/gi,'/');
  6. var img = new Image();
  7.     img.src = 'file://'+path.replace(/\\/gi,'/');
  8.     var size_pic = document.getElementById(previewName).fileSize;
  9.     str = "File size="+size_pic;
  10.       return (str);
  11. }
my HTML who call it:

[HTML]<form name="myform" method="post" action="<myaction>">
<input name="upload_file" type="file" id="upload_file" size="28" value="" onChange="javascript:imageSize(document.myform.upl oad_file, 'tmpImg')"><br>
<img src="tmp.gif" name="tmpImg" width="1" height="1"/>
<input type="Submit" name="upload" value=”upload">
</form>[/HTML]

If I test my function offline it always manage correctly. Unfortunately, when I test it online (i.e. when I copy the html with the above javascript on my webserver Apache), it manages only if files I upload are little. Normally it returns -1!

Could someone help me to find the problem?
Nov 21 '07 #1
13 4756
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

You can't check file size with JavaScript. For local files in non-IE browsers, you won't even manage an image preview.
Nov 21 '07 #2
marfola
13
Thanks, but why my script run correctly if I try to load little images (lesser than 100KB)?
Nov 23 '07 #3
acoder
16,027 Expert Mod 8TB
Which browsers are you testing on?
Nov 26 '07 #4
marfola
13
I'm testing on IE 7, but it would be better if my script run correctly also on Firefox
Nov 26 '07 #5
acoder
16,027 Expert Mod 8TB
This will not work at all in Firefox.

Test it - it won't work even for small images.

JavaScript is not supposed to be able to access the local file system (which includes images).
Nov 26 '07 #6
marfola
13
Do you have some other suggestions to solve my problem, i.e. test image size before upload ?
Nov 27 '07 #7
acoder
16,027 Expert Mod 8TB
Is this to save bandwidth?
Nov 27 '07 #8
marfola
13
Not only, I would like to send a file size exceed message to user, before to change page and before to force him to wait for a long time.
Nov 28 '07 #9
acoder
16,027 Expert Mod 8TB
This won't be possible with JavaScript.

I know PHP has a feature where you can specify a size as a hidden field in the form and it won't upload if it exceeds that size. Other languages may have similar features.
Nov 28 '07 #10
marfola
13
Thanks a lot! I use PHP for my application. Could you give me more details?
Nov 28 '07 #11
acoder
16,027 Expert Mod 8TB
Thanks a lot! I use PHP for my application. Could you give me more details?
Something like:[HTML]<input type="hidden" value="1000000" name="MAX_FILE_SIZE">[/HTML]
See this page
Nov 28 '07 #12
marfola
13
I just use this field:

[html]<input type="hidden" name="MAX_FILE_SIZE" value="2097152">[/html]

but it doesn't allow me to give a message to user before upload, it only avoids the file to be uploaded after user click on submit button.

Do you know a way to check if this field is compatible with the "file" field?
Nov 28 '07 #13
acoder
16,027 Expert Mod 8TB
What you can do is use an iframe and attempt an upload to the iframe. If the file size is too large, you can output something which you can check for and display an error message accordingly.
Nov 28 '07 #14

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

Similar topics

4
by: Yaron Cohen | last post by:
Hi, I would like to ask for you help. I have a page that contains few JS files (please see below). The problem is that sometimes one of the files is not loaded (I am using IE 5.5). I get...
4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
8
by: rdlebreton | last post by:
Hi, Folks! I've been trying to develop my own version of these draggable layers and I have been limiting myself to IE6...for now. I have looked at some other examples to get ideas of creating...
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...
4
by: G0ng | last post by:
Hello. I have a form with two input file fields for uploading file: <form name="mynews" action="news.php?action=update&add=<?php print $id;?>" method="post" enctype="multipart/form-data"> ...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
2
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my...
2
coolv
by: coolv | last post by:
Hello I have problem in my page that the dropdown box is not displaying data according to selection of first dropdown.Please help me. Below is my code. thanks.............. <?php ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.