Skip to content

Instantly share code, notes, and snippets.

@exinferis
Created December 5, 2012 16:24
Show Gist options
  • Save exinferis/4217117 to your computer and use it in GitHub Desktop.
Save exinferis/4217117 to your computer and use it in GitHub Desktop.
request S3 Credentials and build formdata
@_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