Skip to content

Instantly share code, notes, and snippets.

@lloyd
Created July 31, 2009 19:43
Show Gist options
  • Save lloyd/159394 to your computer and use it in GitHub Desktop.
Save lloyd/159394 to your computer and use it in GitHub Desktop.
<html>
<head><title>Zip Uploads</title></head>
<body>
<p>Select one or more files for your first set to <em>ZIP</em> them before upload.</p>
<input id="sel" type="button" onclick="setButton(1)"value="Select Files...">
<input id="up" type="button" value="Upload File" disabled>
<p id="files">[<em>Select files above</em>]</p>
<p id="zip"></p>
<p>Select one or more files for your second set to <em>ZIP</em> them before upload.</p>
<input id="sel2" type="button" onclick="setButton(2)"value="Select Files...">
<input id="up2" type="button" value="Upload File" disabled>
<p id="files2">[<em>Select files above</em>]</p>
<p id="zip2"></p>
</body>
<script type="text/javascript"
src="http://bp.yahooapis.com/2.3.1/browserplus-min.js"></script>
<script type="text/javascript">
var ZippedFiles = null,
elFile,
elZip,
elSel,
elUp;
function setButton(myButton)
{
elFile = document.getElementById(myButton==1?"files":"files2");
elZip = document.getElementById(myButton==1?"zip":"zip2");
elSel = document.getElementById(myButton==1?"sel":"sel2");
elUp = document.getElementById(myButton==1?"up":"up2");
} setButton(1);
function bytes(size) {
var i, units = [' B',' KB',' MB', ' GB',' TB'];
for (i = 0; size > 1024; i++) { size /= 1024; }
return Math.round(size*10)/10 + units[i];
}
function selectFiles(e) {
BrowserPlus.FileBrowse.OpenBrowseDialog({"includeGestureInfo":true}, function(res) {
var i, files, origSize=0;
ZippedFiles = null;
if (res.success) {
// using "files" here to calculate size before zipping
files = res.value.files;
for (i = 0; i < files.length; i++) {
origSize += files[i].handle.size
}
elFile.innerHTML = ""+ files.length +
" file(s) selected, Original Size=" + bytes(origSize);
elZip.innerHTML = "Zip Size=<em>zipping...</em>";
// using "actualSelection" to give ZIP the files user explictly
// chose, otherwise ZIPPER would recurse folder and get dups
BrowserPlus.Zipper.createZip({files: res.value.actualSelection}, function (res) {
var msg, pc, zf;
if (res.success) {
zf = res.value.zipFile;
pc = Math.round((1 - (zf.size / origSize))*100);
msg = "Zip Size=" + bytes(zf.size) +
" - a savings of " + pc + "% or " +
bytes(origSize - zf.size);
ZippedFiles = zf;
elUp.disabled = false;
} else {
msg = "ERROR zipping files";
}
elZip.innerHTML = msg;
});
}
});
return false;
}
function uploadFiles(e) {
elFile.innerHTML ="Uploading...";
elZip.innerHTML = "";
if (ZippedFiles != null) {
BrowserPlus.Uploader.upload(
{
url: "upload.php",
files: {"file": ZippedFiles}
},
function(res) {
if (res.success) {
elFile.innerHTML ="Uploaded! ";
elZip.innerHTML = res.value.body;
//elZip.innerHTML = "<pre>" + res.value.body + "</pre>";
} else {
elZip.innerHTML = "ERROR: " + res.error;
}
});
}
return false;
}
BrowserPlus.init(function(res) {
if (res.success) {
BrowserPlus.require({
services: [
{ service: "Uploader", version: "3", minversion: "3.1.5" },
{ service: "FileBrowse", version: "1" },
{ service: "Zipper", version: "1"}
]}, function(res) {
if (res.success) {
setButton(1);
elSel.onclick = function () {
setButton(1);
selectFiles();
};
elUp.onclick = function () {
setButton(1);
uploadFiles();
};
setButton(2);
elSel.onclick = function () {
setButton(2);
selectFiles();
};
elUp.onclick = function () {
setButton(2);
uploadFiles();
};
} else {
alert("Call to BrowserPlus.require failed: " + res.error);
}
});
} else {
alert("Call to BrowserPlus.init failed: " + res.error);
}
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment