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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex