72

It seems like I have not clearly communicated my problem. I need to send a file (using AJAX) and I need to get the upload progress of the file using the Nginx HttpUploadProgressModule. I need a good solution to this problem. I have tried with the jquery.uploadprogress plugin, but I am finding myself having to rewrite much of it to get it to work in all browsers and to send the file using AJAX.

All I need is the code to do this and it needs to work in all major browsers (Chrome, Safari, FireFox, and IE). It would be even better If I could get a solution that will handle multiple file uploads.

I am using the jquery.uploadprogress plugin to get the upload progress of a file from the NginxHttpUploadProgressModule. This is inside an iframe for a facebook application. It works in firefox, but it fails in chrome/safari.

When I open the console I get this.

Uncaught ReferenceError: progressFrame is not defined
jquery.uploadprogress.js:80

Any idea how I would fix that?

I would like to also send the file using AJAX when it is completed. How would I implement that?

EDIT:
I need this soon and it is important so I am going to put a 100 point bounty on this question. The first person to answer it will receive the 100 points.

EDIT 2:
Jake33 helped me solve the first problem. First person to leave a response with how to send the file with ajax too will receive the 100 points.

6
  • 2
    I see that it uses browser sniffing to detect Safari (browser sniffing is usually frowned upon), so progressFrame is only used with Safari/Chrome (I don't know why). The part where the error is occurring seems to be where the script tries to reference progressFrame just by looking for its name in the global scope instead of using a function like document.getElementsByName.
    – jhartz
    Feb 1, 2011 at 3:01
  • 1
    I'm not sure what you mean by "send it using AJAX when it is completed" -- Isn't the progress bar representing the progress of the submission? Doesn't that mean that it is already sent once it is complete? Do you mean that you want the original form submission to be driven by AJAX so it doesn't cause a page refresh after?
    – slifty
    Feb 3, 2011 at 6:11
  • Yes. Yes, but it acts like a form submission. Thats exactly what I want. Feb 3, 2011 at 22:39
  • If the question how do you send data with an HTTP POST over XMLHttpRequest using jQuery, then there is jQuery.post
    – karlcow
    Feb 5, 2011 at 13:22
  • 1
    Do the answers below work for you? If not, can you comment on the answers. I'm slightly confused as a result of all of the edits and want to try to solve your problem. Thanks. BTW I included a link for JQuery's Multiple File upload plugin. Feb 7, 2011 at 7:37

2 Answers 2

219
+50

Uploading files is actually possible with AJAX these days. Yes, AJAX, not some crappy AJAX wannabes like swf or java.

This example might help you out: https://webblocks.nl/tests/ajax/file-drag-drop.html

(It also includes the drag/drop interface but that's easily ignored.)

Basically what it comes down to is this:

<input id="files" type="file" />

<script>
document.getElementById('files').addEventListener('change', function(e) {
    var file = this.files[0];
    var xhr = new XMLHttpRequest();
    (xhr.upload || xhr).addEventListener('progress', function(e) {
        var done = e.position || e.loaded
        var total = e.totalSize || e.total;
        console.log('xhr progress: ' + Math.round(done/total*100) + '%');
    });
    xhr.addEventListener('load', function(e) {
        console.log('xhr upload complete', e, this.responseText);
    });
    xhr.open('post', '/URL-HERE', true);
    xhr.send(file);
});
</script>

(demo: http://jsfiddle.net/rudiedirkx/jzxmro8r/)

So basically what it comes down to is this =)

xhr.send(file);

Where file is typeof Blob: http://www.w3.org/TR/FileAPI/

Another (better IMO) way is to use FormData. This allows you to 1) name a file, like in a form and 2) send other stuff (files too), like in a form.

var fd = new FormData;
fd.append('photo1', file);
fd.append('photo2', file2);
fd.append('other_data', 'foo bar');
xhr.send(fd);

FormData makes the server code cleaner and more backward compatible (since the request now has the exact same format as normal forms).

All of it is not experimental, but very modern. Chrome 8+ and Firefox 4+ know what to do, but I don't know about any others.

This is how I handled the request (1 image per request) in PHP:

if ( isset($_FILES['file']) ) {
    $filename = basename($_FILES['file']['name']);
    $error = true;

    // Only upload if on my home win dev machine
    if ( isset($_SERVER['WINDIR']) ) {
        $path = 'uploads/'.$filename;
        $error = !move_uploaded_file($_FILES['file']['tmp_name'], $path);
    }

    $rsp = array(
        'error' => $error, // Used in JS
        'filename' => $filename,
        'filepath' => '/tests/uploads/' . $filename, // Web accessible
    );
    echo json_encode($rsp);
    exit;
}
20
  • 27
    This is a feature available only in XMLHttpRequest2, for a full compatibility list - have a look at caniuse.com/xhr2
    – YS.
    Mar 23, 2012 at 0:34
  • @Rudie Do you have the hotblocks.nl tutorial link? not just the demo Thanks in adv
    – vzhen
    Jan 28, 2013 at 7:18
  • There's no tutorial. Never wrote it. Didn't even make the demo to be this public.
    – Rudie
    Jan 28, 2013 at 17:43
  • 1
    @NeilMartin I've added PHP example code to the end of the answer. Obviously you'll want to do something else with it.
    – Rudie
    Jun 26, 2013 at 10:40
  • 1
    I'm not sure I understand how to make this work? I would be great if you could make a complete working example and demo online somewhere. Or maybe some guidance on how to make it work and what things are doing?
    – drooh
    Sep 21, 2015 at 21:33
19

Here are some options for using AJAX to upload files:

UPDATE: Here is a JQuery plug-in for Multiple File Uploading.

7
  • 2
    i used plUpload in several projects with success (multi file upload with progress, supports Flash/Html5/Silverlight etc) Feb 9, 2011 at 12:02
  • 1
    AjaxFileUpload is a piece of crap. It's not compatible with any version of jquery newer than 1.5. Also the code is wicked ugly.
    – Adam
    Mar 27, 2012 at 0:13
  • 1
    @Adam - Good to know. I guess a lot changes in a year. Our hair gets a little greyer and JavaScript libraries stop working with newer software versions... Mar 28, 2012 at 0:29
  • 1
    To expand @Adam, AjaxFileUpload breaks on ie9+
    – 0plus1
    Feb 25, 2013 at 0:26
  • 1
    @RanaMuhammadUsman - Take another look now. I believe I found the location of the moved plugins. Hope this helps. Jun 23, 2014 at 20:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.