Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file <input type="file"> element
- // policy - from the previous post
- // process - function() // callback
- // complete - function() // when its done
- function upload(file, policy, progress, complete) {
- var xhr = new XMLHttpRequest()
- var formdata = new FormData()
- formdata.append('key', policy.path)
- formdata.append('AWSAccessKeyId', policy.accesskey)
- formdata.append('acl', policy.acl)
- formdata.append('policy', policy.policy)
- formdata.append('signature', policy.signature)
- formdata.append('file', file)
- xhr.upload.addEventListener('progress', function(e) {
- if(progress) {
- progress({
- total : file.size,
- sent : e.loaded,
- percent : Math.round((e.loaded * 100) / e.total),
- value : (e.loaded / e.total)
- })
- }
- }, false)
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- if(complete) {
- complete()
- }
- return;
- }
- }
- xhr.open("POST", "https://s3.amazonaws.com/" + policy.bucket + "/", true)
- xhr.send(formdata)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement