I am sending formdata to PHP using ajax.upload for upload file to Google cloud storage
Javascript:
var ajax = new XMLHttpRequest(); //PROGRESS HANDLER ajax.upload.addEventListener("progress", function(event) { var percent = ((event.loaded / event.total) * 100); console.log(Math.round(percent) + "% uploading... please wait"); }, false); //COMPLETE HANDLER ajax.addEventListener("load", function(event) { console.log(event.target.responseText); }, false); //ERROR HANDLER ajax.addEventListener("error", function(event) { console.log("Upload Failed"); }, false); //ABORT HANDLER ajax.addEventListener("abort", function(event) { console.log("Upload Aborted"); }, false); ajax.open("POST", "api/storage.php"); ajax.send(formdata); PHP:
$_SESSION['storedBytes'] = 0; $_SESSION['fileSize'] = $file["size"]; $uploader = $bucket->getResumableUploader( fopen($fileTmpLoc, 'r'), [ 'predefinedAcl' => 'publicRead', 'name' => $_POST['uniqueName'], 'resumable' => true, 'chunkSize' => 262144, 'uploadProgressCallback' => 'uploadProgress' ] ); try { $object = $uploader->upload(); } catch (GoogleException $ex) { $resumeUri = $uploader->getResumeUri(); $object = $uploader->resume($resumeUri); } function uploadProgress($storedBytes) { if (isset($_SESSION['storedBytes'])) { $_SESSION['storedBytes'] += $storedBytes; } else { $_SESSION['storedBytes'] = $storedBytes; } $storedBytes = $_SESSION['storedBytes']; $totalSize = $_SESSION['fileSize']; $_SESSION['progress'] = ($storedBytes*100)/$totalSize; echo "Progress ".$_SESSION['progress']; } And i receive the correct progress value in uploadProgress function but how to send this value to ajax request response asynchronously or how to show progress in this situation.