Julie Rokk
Julie Rokk

Reputation: 560

read filesize while uploading it through ajax request

i'm trying to read the filesize of a file while uploading it this way

1 . start an $.ajax(); request to start a server to server downloading of file (i'm using php fopen + stream_copy_to_stream );

2 . start a second $a.ajax(); request each tot time and try to read the current filesize till the end of download process;

unfortunely this don't work as expected.

both the requests are made correctly but the filesize is read only after the file is fully transfered. so i get all the alert message at the end of the process instead of meanwhile

i guess i'm doing something that can't be done? or i'm just missing something?

pseudo code:

var uploadStart = 0;
function startUploadProgressBar(file) {
    var file = file;
    uploadStart = setInterval(function() {
        $.ajax({
            data: 'uploadProgress=1&file=' + file,
            success: function(json) {
                alert(json.filesize)
            }
        });

    },
    1000);
}
$('#submit').click(function() {
    var file = somevar;
    startUploadProgressBar(file);
    $.ajax({
        success: function(json) {
            clearInterval(uploadStart);
        }
    });
    return false;
});

Upvotes: 0

Views: 566

Answers (2)

janoliver
janoliver

Reputation: 7824

Just for uploading files via ajax, you can consider using the jQuery forms plugin. It supports progress bars, as far as I know.

If you want to stick to your method, read through the comments of the function's manual page in the PHP manual. There are alternatives that can be modified outputting states of the transfer.

Upvotes: 1

Joe C.
Joe C.

Reputation: 1538

Why not just have the PHP page return the size of the file itself using filesize()? That way you don't need to poll repeatedly.

Upvotes: 0

Related Questions