By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

AJAX + php + file uploading

P: n/a
Hi

I'm writing script using AJAX and PHP to upload files. I'm trying to
create progress bar to show how many bytes have been already uploaded to
server.

Can I get the total file size (using JS) before sending it to server or
immediately after start sending it?

And how to obtain amount o bytes that have been already transfered to
server using JS? I know how to do it in PHP (XMLHttpRequest to php file
that counts temporary file size), but is it possible in JS?

Thanks
Jan 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Igor said the following on 1/29/2006 2:43 PM:
Hi

I'm writing script using AJAX and PHP to upload files.
Why? The browser has that built in functionality already and that
support is a lot wider than AJAX is.

I have used AJAX also, it works wonders on dirty tile.
I'm trying to create progress bar to show how many bytes have
been already uploaded to server.
Why do people feel the need to reinvent the wheel and start with a
square one when the round one is fine?

Can I get the total file size (using JS) before sending it to server or
immediately after start sending it?
No.
And how to obtain amount o bytes that have been already transfered to
server using JS? I know how to do it in PHP (XMLHttpRequest to php file
that counts temporary file size), but is it possible in JS?


Run a separate request using setTimeout to poll the server. It will tell
you the size loaded so far, and it will tell you the total size. From
there, do the Math.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 29 '06 #2

P: n/a
Igor wrote:
Can I get the total file size (using JS) before sending it to server
or immediately after start sending it?


You could do a new xmlhttp request after the file has been uploaded and call
its getResponseHeader method as follows:

xmlhttp.open("HEAD", "uploaded.file", true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
alert(xmlhttp.getResponseHeader('Content-Length'));
}
}

This will alert the size of the uploaded file in bytes when:

o The uploaded file is accessible by the world;
o The Content-Length header is returned by your web server.
JW
Jan 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.