Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2012 20:33
Show Gist options
  • Select an option

  • Save anonymous/2696607 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/2696607 to your computer and use it in GitHub Desktop.
YUI Uploader with Django CSRF support
// example from here: http://yuilibrary.com/yui/docs/uploader/uploader-multiple.html
YUI({filter:"raw"}).use("uploader", "cookie", function(Y) {
var csrf_value = Y.Cookie.get("csrftoken");
Y.one("#overallProgress").set("text", "Uploader type: " + Y.Uploader.TYPE);
if (Y.Uploader.TYPE != "none" && !Y.UA.ios) {
var uploader = new Y.Uploader({width: "250px",
height: "35px",
multipleFiles: true,
swfURL: "http://yui.yahooapis.com/3.5.1/build/uploader/assets/flashuploader.swf?t=" + Math.random(),
uploadURL: "http://www.yswfblog.com/upload/simpleupload.php",
simLimit: 2,
postVarsPerFile: {'csrfmiddlewaretoken': csrf_value}
});
var uploadDone = false;
uploader.render("#selectFilesButtonContainer");
uploader.after("fileselect", function (event) {
var fileList = event.fileList;
var fileTable = Y.one("#filenames tbody");
if (fileList.length > 0 && Y.one("#nofiles")) {
Y.one("#nofiles").remove();
}
if (uploadDone) {
uploadDone = false;
fileTable.setContent("");
}
Y.each(fileList, function (fileInstance) {
fileTable.append("<tr id='" + fileInstance.get("id") + "_row" + "'>" +
"<td class='filename'>" + fileInstance.get("name") + "</td>" +
"<td class='filesize'>" + fileInstance.get("size") + "</td>" +
"<td class='percentdone'>Hasn't started yet</td>");
});
});
uploader.on("uploadprogress", function (event) {
var fileRow = Y.one("#" + event.file.get("id") + "_row");
fileRow.one(".percentdone").set("text", event.percentLoaded + "%");
});
uploader.on("uploadstart", function (event) {
uploader.set("enabled", false);
Y.one("#uploadFilesButton").addClass("yui3-button-disabled");
Y.one("#uploadFilesButton").detach("click");
});
uploader.on("uploadcomplete", function (event) {
var fileRow = Y.one("#" + event.file.get("id") + "_row");
fileRow.one(".percentdone").set("text", "Finished!");
});
uploader.on("totaluploadprogress", function (event) {
Y.one("#overallProgress").setContent("Total uploaded: <strong>" + event.percentLoaded + "%" + "</strong>");
});
uploader.on("alluploadscomplete", function (event) {
uploader.set("enabled", true);
uploader.set("fileList", []);
Y.one("#uploadFilesButton").removeClass("yui3-button-disabled");
Y.one("#uploadFilesButton").on("click", function () {
if (!uploadDone && uploader.get("fileList").length > 0) {
uploader.uploadAll();
}
});
Y.one("#overallProgress").set("text", "Uploads complete!");
uploadDone = true;
});
Y.one("#uploadFilesButton").on("click", function () {
if (!uploadDone && uploader.get("fileList").length > 0) {
uploader.uploadAll();
}
});
}
else {
Y.one("#uploaderContainer").set("text", "We are sorry, but the uploader technology is not supported" +
" on this platform.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment