Skip to main content

How to send FormData objects with Ajax-requests in jQuery?



The XMLHttpRequest Level 2 standard (still a working draft) defines the FormData interface. This interface enables appending File objects to XHR-requests (Ajax-requests).





Btw, this is a new feature - in the past, the "hidden-iframe-trick" was used (read about that in my other question ).





This is how it works (example):







var xhr = new XMLHttpRequest(),

fd = new FormData();



fd.append( 'file', input.files[0] );

xhr.open( 'POST', 'http://example.com/script.php', true );

xhr.onreadystatechange = handler;

xhr.send( fd );







where input is a <input type="file"> field, and handler is the success-handler for the Ajax-request.





This works beautifully in all browsers (again, except IE).





Now, I would like to make this functionality work with jQuery. I tried this:







var fd = new FormData();

fd.append( 'file', input.files[0] );



$.post( 'http://example.com/script.php', fd, handler );







Unfortunately, that won't work (an "Illegal invocation" error is thrown - screenshot is here ). I assume jQuery expects a simple key-value object representing form-field-names / values, and the FormData instance that I'm passing in is apparently incompatible.





Now, since it is possible to pass a FormData instance into xhr.send() , I hope that it is also possible to make it work with jQuery.








Update:





I've created a "feature ticket" over at jQuery's Bug Tracker. It's here: http://bugs.jquery.com/ticket/9995





I was suggested to use an "Ajax prefilter"...








Update:





First, let me give a demo demonstrating what behavior I would like to achieve.





HTML:







<form>

<input type="file" id="file" name="file">

<input type="submit">

</form>







JavaScript:







$( 'form' ).submit(function ( e ) {

var data, xhr;



data = new FormData();

data.append( 'file', $( '#file' )[0].files[0] );



xhr = new XMLHttpRequest();



xhr.open( 'POST', 'http://hacheck.tel.fer.hr/xml.pl', true );

xhr.onreadystatechange = function ( response ) {};

xhr.send( data );



e.preventDefault();

});







The above code results in this HTTP-request:





multipartformdata





This is what I need - I want that "multipart/form-data" content-type!








The proposed solution would be like so:







$( 'form' ).submit(function ( e ) {

var data;



data = new FormData();

data.append( 'file', $( '#file' )[0].files[0] );



$.ajax({

url: 'http://hacheck.tel.fer.hr/xml.pl',

data: data,

processData: false,

type: 'POST',

success: function ( data ) {

alert( data );

}

});



e.preventDefault();

});







However, this results in:





wrongcontenttype





As you can see, the content type is wrong...



Source: Tips4all

Comments

  1. I think you cant do it in ajax to support all the browsers, I might say good to check this ajax uploader plugin to see how they have done it http://valums.com/ajax-upload/

    ReplyDelete
  2. Here's some working code for FormData

    and the Mozilla FormData Documentation

    oh and here's one for jQuery

    Hope this helps

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.