13

Possible Duplicate:
How can I upload files asynchronously with JQuery?

I'm submiting my forms like this.

  var url = event.currentTarget.action;
  var values = $(this).serialize();

  $.post(url, values, function (data) {
      //some code
  });

Now I have a form with a file upload input. With this code the file isn't uploaded.

How can I include the file into this ajax request ? I don't want to use any plugin if possible(except jQuery).

Cœur
  • 37,241
  • 25
  • 195
  • 267
user256034
  • 4,289
  • 5
  • 37
  • 44

1 Answers1

2

You can only upload files with AJAX but only in some modern browsers. I know it works in Firefox and Chrome (That's all I've tested so far).

Theres some good info here about it: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

As an alternative there's a great plugin for that sort of thing:

http://jquery.malsup.com/form/

This part may be of relevance to you:

http://jquery.malsup.com/form/#file-upload

It uses iframes to post to to upload in a background style of way.

beaver
  • 523
  • 1
  • 9
  • 20
Tomgrohl
  • 1,767
  • 10
  • 16