Created
April 26, 2012 12:41
-
-
Save Enome/2499300 to your computer and use it in GitHub Desktop.
Fake async file upload using an iframe.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$( 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 ); | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var contentType = req.headers.accept.indexOf('application/json') !== -1 ? 'application/json' : 'text/plain'; | |
res.header('Content-Type', contentType); | |
res.send(JSON.stringify(data)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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