Skip to content

Instantly share code, notes, and snippets.

@Enome
Created April 26, 2012 12:41
Show Gist options
  • Save Enome/2499300 to your computer and use it in GitHub Desktop.
Save Enome/2499300 to your computer and use it in GitHub Desktop.
Fake async file upload using an iframe.
$( function(){
var form = $('<form>');
var iframe = $('<iframe name="upload_iframe">');
var file_upload = $('.file-upload');
// Form
form.attr("target", "upload_iframe");
form.attr("action", '/upload');
form.attr("method", "post");
form.attr("enctype", "multipart/form-data");
form.attr("encoding", "multipart/form-data");
// File upload
file_upload.change( function(){
iframe.unbind('load').bind('load', function() {
var response = iframe.contents().find('body').text();
});
form.submit();
});
// Iframe
iframe.attr('id', 'upload_iframe');
iframe.hide();
// Add elements
file_upload.after( iframe );
file_upload.after( form );
form.append( file_upload );
} );
var contentType = req.headers.accept.indexOf('application/json') !== -1 ? 'application/json' : 'text/plain';
res.header('Content-Type', contentType);
res.send(JSON.stringify(data));
<input type='file' class='file-upload' name='image' />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment