Created
November 14, 2011 17:42
-
-
Save jfgomez86/1364568 to your computer and use it in GitHub Desktop.
Javascript for uploading using https://github.com/nathancolgate/s3-swf-upload-plugin
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="swf_upload"> | |
<strong>New Upload:</strong> | |
<div id="browse"> | |
<%= raw s3_swf_upload_tag( | |
fileTypes: "*.csv", | |
queueSizeLimit: 1, | |
selectMultipleFiles: false, | |
onQueueEmpty: "window.uploader.queueEmpty();", | |
onFileAdd: "window.uploader.fileAdd(file);", | |
onUploadingStart: "window.uploader.uploadingStart();", | |
onUploadingFinish: "window.uploader.uploadingFinish();", | |
onUploadProgress: "window.uploader.uploadProgress(progress_event);", | |
onUploadComplete: "window.uploader.uploadComplete(upload_options, event);" | |
) %> | |
</div> | |
<div id="file-list"> | |
<ul id="queue"> | |
</ul> | |
</div> | |
<input type="submit" value="Start Uploading" id="start_upload" /> | |
</div> |
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 position = $("#browse").position(); | |
// This puts the queue on the same place as the flash button. Pure Eyecandy. | |
$("#queue").css({ | |
"top": position.top, | |
"left": position.left, | |
"position": "absolute", | |
"display": "none" | |
}); | |
$("#start_upload").click(function (evt) { | |
evt.preventDefault(); | |
s3_swf_1_object.startUploading(); | |
$(this).val("Please wait..."); | |
$(this).attr("disabled", "disabled"); | |
}); | |
var uploader = { | |
// s3_swf_upload handlers | |
queueEmpty: function () { | |
alert("You have to select 1 file.") | |
}, | |
fileAdd: function (file) { | |
$("#queue").html(function () { | |
var size_in_mb = Math.round(file.size / (1024 * 1024) * 100) / 100, | |
li = $("<li>").html(file.name + " (" + size_in_mb + " mb) "), | |
a = $("<a href='javascript:;'>").html("remove"); | |
$(s3_swf_1_object.obj()).css("visibility", "hidden"); | |
$("#queue").show(); | |
a.click(function (evt) { | |
evt.preventDefault(); | |
s3_swf_1_object.clearQueue(); | |
$(this).parent("li").remove(); | |
$("#queue").hide(); | |
$(s3_swf_1_object.obj()).css("visibility", "visible"); | |
}); | |
li.append(a); | |
return li; | |
}); | |
}, | |
uploadingStart: function () { | |
$("#queue li a").replaceWith(function () { | |
var progress = $("<strong id='progress'>").html("Uploading: 0 %"); | |
return progress; | |
}); | |
}, | |
uploadingFinish: function () { | |
$("#queue li #progress").html(function () { | |
return "Upload Done!"; | |
}); | |
}, | |
uploadProgress: function (progress_event) { | |
$("#progress").html(function () { | |
var percentage = parseInt(progress_event.bytesLoaded) / parseInt(progress_event.bytesTotal); | |
return "Uploading: " + Math.floor(percentage * 100) + " %" | |
}); | |
}, | |
uploadComplete: function (upload, evt) { | |
var form = $("<form action='/admin/imports' method='post'>"); | |
form.append("<input type='hidden' name='file_name' value='" + upload.FileName + "'>"); | |
$("body").append(form); | |
form.submit(); | |
} | |
} | |
window.uploader = uploader; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment