472,973 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

FileSize - image

Ken
I would like to measure the fileSize of the image (without uploading it -
php).

I use:
var size_pic = document.getElementById('num1').childNodes[0].fileSize;
alert("size = " + size_pic);
which works on IE with one exception.

With a first time displayed image, the script displays a size= -1 The image
is displayed on the first pass. The fileSize script is located after the
img script.
var image_display=document.createElement('img');
image_display.src='file://' + field;
image_display.name = 'pict';
document.getElementById('num1').appendChild(image_ display);

The second pass is correct. size = 11479

Is this a cache problem? The first pass caches the file, the second pass
reads the file?

I tried running - var size_pic
=document.getElementById('num1').childNodes[0].fileSize; - twice but that
made no difference.

Is there a work around for this problem.

Is there a better way to measure filesize in JavaScript?

Have a good day!

Ken
Jul 23 '05 #1
4 5407
Ken wrote:
I would like to measure the fileSize of the image (without uploading it -


You asked this question a couple of weeks ago, the answer is still the
same - you can't. Read your thread of 9 Oct.

Zif.
Jul 23 '05 #2
Ken wrote:
I would like to measure the fileSize of the image (without uploading it -
php).

I use:
var size_pic = document.getElementById('num1').childNodes[0].fileSize;
alert("size = " + size_pic);
which works on IE with one exception.

With a first time displayed image, the script displays a size= -1 The image
is displayed on the first pass. The fileSize script is located after the
img script.
var image_display=document.createElement('img');
image_display.src='file://' + field;
image_display.name = 'pict';
document.getElementById('num1').appendChild(image_ display);

The second pass is correct. size = 11479

Is this a cache problem? The first pass caches the file, the second pass
reads the file?


No, the problem is that the file has not yet loaded by the time you attempt to
access it's size. Make the display of it's size dependant on the onload event of
the image.

var image_display=document.createElement('img');
image_display.onload = function() {
alert(this.fileSize);
}
image_display.src='file://' + field;
image_display.name = 'pict';
document.getElementById('num1').appendChild(image_ display);

Of course, you'll need to replace alert(this.fileSize) with something more
useful, perhaps create another node under the image and display the size there.

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

Jul 23 '05 #3
Ken
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:41***************@agricoreunited.com...
Ken wrote:
I would like to measure the fileSize of the image (without uploading it - php).

I use:
var size_pic = document.getElementById('num1').childNodes[0].fileSize;
alert("size = " + size_pic);
which works on IE with one exception.

With a first time displayed image, the script displays a size= -1 The image is displayed on the first pass. The fileSize script is located after the img script.
var image_display=document.createElement('img');
image_display.src='file://' + field;
image_display.name = 'pict';
document.getElementById('num1').appendChild(image_ display);

The second pass is correct. size = 11479

Is this a cache problem? The first pass caches the file, the second pass reads the file?
No, the problem is that the file has not yet loaded by the time you

attempt to access it's size. Make the display of it's size dependant on the onload event of the image.

var image_display=document.createElement('img');
image_display.onload = function() {
alert(this.fileSize);
}
image_display.src='file://' + field;
image_display.name = 'pict';
document.getElementById('num1').appendChild(image_ display);

Of course, you'll need to replace alert(this.fileSize) with something more
useful, perhaps create another node under the image and display the size there.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Grant,
Thanks for the suggestion.
Works great.
Ken


Jul 23 '05 #4
Ken
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:41***************@agricoreunited.com...
Ken wrote:
I would like to measure the fileSize of the image (without uploading it - php).

I use:
var size_pic = document.getElementById('num1').childNodes[0].fileSize;
alert("size = " + size_pic);
which works on IE with one exception.

With a first time displayed image, the script displays a size= -1 The image is displayed on the first pass. The fileSize script is located after the img script.
var image_display=document.createElement('img');
image_display.src='file://' + field;
image_display.name = 'pict';
document.getElementById('num1').appendChild(image_ display);

The second pass is correct. size = 11479

Is this a cache problem? The first pass caches the file, the second pass reads the file?
No, the problem is that the file has not yet loaded by the time you

attempt to access it's size. Make the display of it's size dependant on the onload event of the image.

var image_display=document.createElement('img');
image_display.onload = function() {
alert(this.fileSize);
}
image_display.src='file://' + field;
image_display.name = 'pict';
document.getElementById('num1').appendChild(image_ display);

Of course, you'll need to replace alert(this.fileSize) with something more
useful, perhaps create another node under the image and display the size there.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Grant,

This works but not like I want.

I am trying to determine the file size to test for size < max spec. The
script works for the first image but not for a second image which replaces
the first image if it is over spec.

Removing the onload... the script works but will not measure fileSize on the
first pass.

Any suggestions.

Ken

<input type=file size=65 name="picture1" onChange="image_size(this.value);"
Id="pt1">
<div id="image_size_display"></div>
<script type="text/javascript">
// Image size to be under max limit
function image_size(field){
if ((document.createElement) && (document.getElementById)) {
var image_display=document.createElement('img');
image_display.onload = function() { image_size = this.fileSize; }
image_display.src='file://' + field;
image_display.name = 'pict';
image_display.alt = 'image';

document.getElementById('image_size_display').appe ndChild(image_display);

alert("image_size = " + image_size);

if(image_size >2000000){alert("This picture ( file ) is geater than
2,000,000.\n\n" + "Select another picture or reduce the size of the picture
( file )")};
alert("test");

document.getElementById('image_size_display').remo veChild(image_display);
} }
</script>
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
// PROCESS XML CONTENT INTO DYNAMICALLY-NAMED ARRAYS foreach (array('mime', 'state', 'country') as $val) { $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);...
6
by: John | last post by:
Hi all, I'd like to evaluate dynamicaly the size of a dynamic file. I use the filesize() function... this is my code : $my_size =...
4
by: Marek Möhling | last post by:
My server (Apache/1.3.28 - PHP/4.3.3) is configured to receive gzipped data via: Header append Accept-Encoding "gzip, deflate" PHP is configured to send gzipped data via: php_value...
5
by: David | last post by:
Not sure whether this is something Im doing wrong or just something the filesize() function doesnt support but... <? $fsize1 = filesize("foo.txt"); $fp = fopen("foo.txt",a); $time = time();...
2
by: IWP506 | last post by:
I just CANNOT figure out what is wrong with this! ------ $op1f = fopen("./test.txt","r"); $op1 = fread($op1f, filesize($op1f)); ------
3
by: Arjen | last post by:
Hello, When I have read a file with x.xxx.xxx.xxx bytes it is sometimes handy to show it in an diverent format than bytes. Is there a handy way to do this? Or is there a function for it? ...
3
by: mosscliffe | last post by:
I get a list of filenames from a directory with the following code, but I can not work out how to get the filesize of the filename I have just found. I guess it is fileinfo.length, but I can not...
2
by: Eric Layman | last post by:
I have checked this setting in my php.ini allow_url_fopen = On It is ON be default. According to the PHP documentation: it stated that as of php5, filesize supports the reading of remote...
3
ganesanji
by: ganesanji | last post by:
hi all, I have created a simple downloading script for a file which is given below$f="images/52219-4.bmp"; $ftype = mime_content_type($f); $fsize= filesize($f); header("Content-type:...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.