Skip to content

Commit

Permalink
trigger progress event on fileupload progress
Browse files Browse the repository at this point in the history
  • Loading branch information
baugarten committed May 3, 2015
1 parent 47de8f5 commit 5651f6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ $('#myS3Uploader').bind 's3_uploads_start', (e) ->
alert("Uploads have started")
```

#### Successfull upload
#### Successful upload
When a file has been successfully uploaded to S3, the `s3_upload_complete` is triggered on the form. A `content` object is passed along with the following attributes :

* `url` The full URL to the uploaded file on S3.
Expand All @@ -221,6 +221,15 @@ $('#myS3Uploader').bind "s3_upload_complete", (e, content) ->
$('#someHiddenField').val(content.url)
```

#### Upload Progress
When uploading to S3, we receive periodic progress events that expose the percentage of the file(s) uploaded to s3. This is how we update the progress bar. This could be useful for displaying more fine-grained information about the upload, like an ETA or rate.

```coffeescript
$('#myS3Uploader').bind "s3_upload_progress", (e, data) ->
remaining_time = (data.total - data.loaded) / data.bitrate
$('#someHiddenField').val(remaining_time)
```

#### Failed upload
When an error occured during the transferm the `s3_upload_failed` is triggered on the form with the same `content` object is passed for the successful upload with the addition of the `error_thrown` attribute. The most basic way to handle this error would be to display an alert message to the user in case the upload fails :
```coffeescript
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/s3_direct_upload.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ $.fn.S3Uploader = (options) ->

progress: (e, data) ->
if data.context
$uploadForm.trigger("s3_uploads_progress", [e, data])
progress = parseInt(data.loaded / data.total * 100, 10)
data.context.find('.bar').css('width', progress + '%')

Expand Down

0 comments on commit 5651f6f

Please sign in to comment.