AJAX + php + file uploading

I

Igor

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
 
R

Randy Webb

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.
 
J

Janwillem Borleffs

Igor said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,832
Latest member
UtaHetrick

Latest Threads

Top