Created
October 14, 2014 09:38
-
-
Save jude90/9682bf361c792a33b837 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* Adapted for Swift by Chmouel Boudjnah <[email protected]> | |
Original: http://www.ioncannon.net/programming/1539/direct-browser-uploading-amazon-s3-cors-fileapi-xhr2-and-signed-puts/ | |
*/ | |
function sw_md5(blob, fn){ | |
_reader.onloadend = function(evt) { | |
if (evt.target.readyState == FileReader.DONE) { // DONE == 2 | |
var md5sum = md5(evt.target.result); | |
fn(md5sum); | |
} | |
}; | |
_reader.onerror = function(evt) { | |
alert("Error: " + evt.target.error.code); | |
}; | |
_reader.readAsBinaryString(blob); | |
}; | |
function createCORSRequest(method, url) | |
{ | |
var xhr = new XMLHttpRequest(); | |
if ("withCredentials" in xhr) | |
{ | |
xhr.open(method, url, true); | |
} | |
else if (typeof XDomainRequest != "undefined") | |
{ | |
xhr = new XDomainRequest(); | |
xhr.open(method, url); | |
} | |
else { | |
xhr = null; | |
} | |
return xhr; | |
} | |
function handleFileSelect(evt) | |
{ | |
setProgress(0, 'Upload started.'); | |
var files = evt.target.files; | |
var output = []; | |
for (var i = 0, f; f = files[i]; i++) | |
{ | |
sw_md5(f, function(md5sum){ | |
alert("md5: "+md5sum) | |
uploadFile(f); | |
}); | |
} | |
} | |
function uploadFile(file) | |
{ | |
authUrl=document.getElementById('authurl').value; | |
container=document.getElementById('container').value; | |
token=document.getElementById('authtoken').value; | |
signedURL=authUrl + "/" + container + "/" + file.name; | |
uploadToSW(file, signedURL, token); | |
} | |
/** | |
* Use a CORS call to upload the given file to Swift. Assumes the url | |
* parameter has been signed and is accessable for upload. | |
*/ | |
function uploadToSW(file, url, token) | |
{ | |
var xhr = createCORSRequest('PUT', url); | |
if (!xhr) | |
{ | |
setProgress(0, 'CORS not supported'); | |
} | |
else | |
{ | |
xhr.onload = function() | |
{ | |
if(xhr.status == 200 || xhr.status == 201) | |
{ | |
setProgress(100, 'Upload completed.'); | |
} | |
else | |
{ | |
setProgress(0, 'Upload error: ' + xhr.status); | |
} | |
}; | |
xhr.onerror = function() | |
{ | |
setProgress(0, 'XHR error.'); | |
}; | |
xhr.upload.onprogress = function(e) | |
{ | |
if (e.lengthComputable) | |
{ | |
var percentLoaded = Math.round((e.loaded / e.total) * 100); | |
setProgress(percentLoaded, percentLoaded == 100 ? 'Finalizing.' : 'Uploading.'); | |
} | |
}; | |
// var formData = new FormData(); | |
// formData.append("redirect","http://192.168.99.103") | |
// formData.append("max_file_size", "5373952000") | |
// formData.append("expires", "1409551225") | |
// formData.append("max_file_count","200") | |
// formData.append("signature","b9bbd3a42334a3a3591c486d42f1040649dee026") | |
// formData.append("file1",file) | |
xhr.setRequestHeader('X-Auth-Token', token); | |
// xhr.setRequestHeader('X-Container-Meta-Access-Control-Allow-Origin','*') | |
// xhr.setRequestHeader('Content-Type', file.type); | |
xhr.send(file); | |
} | |
} | |
function setProgress(percent, statusLabel) | |
{ | |
var progress = document.querySelector('.percent'); | |
progress.style.width = percent + '%'; | |
progress.textContent = percent + '%'; | |
document.getElementById('progress_bar').className = 'loading'; | |
document.getElementById('status').innerText = statusLabel; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment