Created
May 31, 2013 17:16
-
-
Save BenjaminAdams/5686442 to your computer and use it in GitHub Desktop.
The iframe upload trick - How to upload files without refreshing the page by submitting the file through a hidden 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
<div id='status'></div> | |
<form id="file-upload-form" name="file-upload-form"> | |
<input type="file" id="upload-doc-file" name="upload-doc-file"> | |
</form> | |
<iframe id="postiframe" name="postiframe"></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
iframeSubmitFile: function() { | |
//adds a spinning loading icon. Icon is from font awesome | |
this.$el.find("#status").html("<i class='icon-spinner icon-spin loading'> </i>"); | |
var form = $('#file-upload-form'); | |
form.attr("action", "/user-upload-doc"); | |
form.attr("method", "post"); | |
form.attr("enctype", "multipart/form-data"); | |
form.attr("target", "postiframe"); | |
//form.attr("target", iframe); | |
form.attr("file", this.$el.find('#upload-doc-file').val()); | |
//example of how to add another value to the post field | |
var audit_id = 5; | |
//dynamically create an input value for the form post | |
var audit_id_input = $("<input>").attr("type", "hidden").attr("name", "audit_id").val(audit_id); | |
//add it to the form | |
form.append($(audit_id_input)); | |
//submit form | |
form.submit(); | |
this.refreshUploadAction(); //reset the upload box | |
this.$el.find("#postiframe").load(function() { | |
//removes the loading icon because the file has finished uploading | |
$("#status").html(""); | |
//having trouble getting the results back from the post | |
// console.log($("#postiframe")) | |
// iframeContents = $("#postiframe")[0].contentWindow.document.body.innerHTML; | |
// console.log(iframeContents) | |
// $("#textarea").html(iframeContents); | |
}); | |
return false; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment