Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom upload progress bar

Tags:

jquery

php

upload

I have seen all the upload progress bar plugins, widgets, etc. – they all suck. They're either too bulky with too much useless code or they don't work.

What I want to know is where I can read up on how to display an easy upload progress indicator. Most browsers have a status progress bar on them below, but it isn't very professional to use just that when dealing with clients.

How does the browser do it? I want to know the internals of how the browser works with for indicating the status of something uploading so that maybe I can make something using PHP & jquery.

Thanks.

like image 844
michael Avatar asked Jun 18 '10 07:06

michael


3 Answers

Since you want to use PHP, I'd start with the uploadprogress extension (PHP doesn't, by default, give you any data about until the upload is completed; this extension provides such server-side functionality). Note that it requires PHP 5.2+ and can be picky about configuration options. Also see its commentary and demo and troubleshooting hints). A short overview available in PHP documentation comments.

Using the extension, you can get some stats on the upload; then you can poll the server through AJAX and update some sort of progress bar.

To be a bit more specific:

  • get an unique identifier for the form, and include it in a hidden field
  • the upload should run in an IFRAME - some browsers won't allow DOM updates to the same page that is running the upload
  • poll the server via AJAX (using the identifier to specify which upload you're interested in) and parse the result (IIRC, you'll get something like "bytes_uploaded => 123, content-length=> 1000")
  • update your progress bar (the way you display it is up to you, I use "x% done" plus a graphical bar)
  • redirect whole page when form uploaded OK

(Oh, and btw, check PHP's upload_max_filesize and post_max_size settings, as both limit the maximum upload size)

like image 169
Piskvor left the building Avatar answered Nov 16 '22 23:11

Piskvor left the building


There is no reliable method to get the progress of a file upload using JavaScript. Support for the XMLHttpRequest upload.onprogress event is missing in many browsers, even with HTML5, you will inevitably need to turn to Flash, or another method of file upload.

After asking this question, I eventually settled on using plupload for my uploading needs.

like image 20
gnarf Avatar answered Nov 17 '22 01:11

gnarf


Javascript/Ecmascript cannot mess with native browser functionalitys (which uses C/C++ mostly along with OS APIs which access TCP/UDP sockets.

To display a progressbar with JS you need serverfeedback. That can be accomplished by using a server polling like COMET for instance. But at this point, it's never that accurate like the browser buildin progressbar. It will change maybe with HTML5 WebSockets.

To have an accurate result, you need a continuous connection. You can fake and gild the client-server request delay, but it's still there. I don't know exactly how Flash handles this issues, but I guess it also does not have connection-oriented stream (Correct me if I'm wrong here).

like image 1
jAndy Avatar answered Nov 17 '22 01:11

jAndy