It seems like you may need to use a FormData
object in your AJAX/pJAX request which can process files. You can read more on the Mozilla Developer Network.
For example, if this is your form:
<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="button" value="Upload" />
</form>
First get the file contents like so:
var formData = new FormData($('form')[0]);
Then you could use a jQuery $.ajax
request or a simple XMLHttpRequest();
like so:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://foo.com/processfile.php");
xhr.send(formData);
Or similarly in jQuery:
$.ajax({
url: 'http://foo.com/processfile.php',
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false
});
On the server side, in processfile.php
, you can receive/display the file contents with:
$file = $_FILES['file']['name'];
This should work with pJAX as it is asynchronous! Just make sure you place this request BEFORE your pJAX request, or if you're using jQuery, you can add it as part of the success callback. For example (without knowing which pJAX library you are using):
$.ajax({
url: 'http://foo.com/processfile.php',
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data) {
$.pjax({url: url, container: '#pjax-container'});
}
});
Edit: If you would like to support IE7+, you need to fallback to using a hidden iframe element for the upload as FormData is only supported in Internet Explorer 10. A great plugin for submitting files which I have tested and works without jQuery is http://fineuploader.com/ which in my opinion is better/easier to use than the https://github.com/malsup/form (jquery.form plugin).