Created
December 5, 2012 16:24
-
-
Save exinferis/4217117 to your computer and use it in GitHub Desktop.
request S3 Credentials and build formdata
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
@_requestCredentials( @format, _fName, ( res ) => | |
# building html5 / formdata object for use with xhr transport | |
formData = new FormData | |
formData.append( "key", "#{res.FileDescriptor}.#{@ext}" ) | |
formData.append( "acl", "public-read" ) | |
formData.append( "Content-Disposition", "" ) | |
formData.append( "Content-Type", res.ContentType ) | |
formData.append( "success_action_status", res.ActionStatus ) | |
formData.append( "AWSAccessKeyId", res.Key ) | |
formData.append( "Policy", res.PolicyBase64 ) | |
formData.append( "Signature", res.Signature ) | |
formData.append( "file", @images[_id] ) | |
try | |
xhr = new XMLHttpRequest | |
xhr.onreadystatechange = => | |
#xhr request has finished - one way or another | |
if xhr.readyState is 4 | |
if xhr.status is 200 or xhr.status is 201 | |
@_uploadFinished( _id, "http://#{res.Bucket}.s3.amazonaws.com/#{res.FileDescriptor}.#{@ext}" ) | |
else | |
@_uploadError( _id, xhr.status ) | |
return | |
xhr.onprogress = ( progress ) => | |
# do something fancy here - maybe an animated progress bar? | |
return | |
xhr.open "POST", "http://#{res.Bucket}.s3.amazonaws.com", true | |
xhr.send formData | |
catch e | |
@_uploadError( _id, e ) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment