Skip to content

Instantly share code, notes, and snippets.

@boxxxie
Created April 17, 2012 17:37
Show Gist options
  • Save boxxxie/2407693 to your computer and use it in GitHub Desktop.
Save boxxxie/2407693 to your computer and use it in GitHub Desktop.
futon's uploadAttachment function
//requires jquery.form.js
//<script src="/_utils/script/jquery.form.js"></script>
/*
<form action="" method="post" id="upload-form">
<h2>Upload Attachment</h2>
<fieldset>
<p class="help">
Please select the file you want to upload as an attachment to this
document. Please note that this will result in the immediate creation of
a new revision of the document, so it's not necessary to save the
document after the upload.
</p>
<table summary=""><tbody><tr>
<th><label>File:</label></th>
<td><input type="file" name="_attachments"></td>
</tr><tr>
<td id="progress" colspan="2">&nbsp;</td>
</tr></tbody></table>
</fieldset>
<div class="buttons">
<input type="hidden" name="_rev" value="3-40b650398c60eb0a8259bbf0ae2ea865">
<button type="submit">Upload</button>
<button type="button" class="cancel">Cancel</button>
</div>
</form>
*/
this.uploadAttachment = function() {
if (page.isDirty) {
alert("You need to save or revert any changes you have made to the " +
"document before you can attach a new file.");
return false;
}
$.showDialog("dialog/_upload_attachment.html", {
load: function(elem) {
$("input[name='_rev']", elem).val(page.doc._rev);
},
submit: function(data, callback) {
if (!data._attachments || data._attachments.length == 0) {
callback({_attachments: "Please select a file to upload."});
return;
}
var form = $("#upload-form");
form.find("#progress").css("visibility", "visible");
form.ajaxSubmit({
url: db.uri + $.couch.encodeDocId(page.docId),
success: function(resp) {
form.find("#progress").css("visibility", "hidden");
page.isDirty = false;
location.href = "?" + encodeURIComponent(dbName) +
"/" + $.couch.encodeDocId(page.docId);
}
});
}
});
}
// someone else's method for saving attachments
//may not work properly
function saveAttachment() {
$db.openDoc("{{id}}", {
success: function(doc) {
var data = {};
data._rev = doc._rev;
data. Attachment = $("#Anhang").val();
$.ajax({
type: "Put",
url: "/evab/" + "{{id}}" + "/" + $("# Attachment ").val() + "?rev=" + doc._rev,
data: data. Attachment,
contentType: "multipart/form-data",
success: function(resp) {
//show attachments in form
}
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment