Ember-cli-file-picker
An addon for ember-cli that provides a component to easily add a filepicker to your ember-cli app.
It supports:
- A preview of the uploaded file
- A dropzone to drag and drop your file
- Currently it only support single file uploads but multiples will come soon
Installation
ember install ember-cli-file-picker
Changelog
0.0.10
- [BUGFIX] Set input to null after select to fix webkit caching. @nicford-gojimo
0.0.9
- [BUGFIX] Restore preview after selecting 'cancel' from the browser file dialog. @quiaro
0.0.8
- [ENHANCEMENT] Add
hideFileInput
option to configure the file input visibility.
0.0.7
- [BUGFIX]
filesAreValid
was never called. @green-arrow
0.0.6
- removes "Binding style attributes..." warning
0.0.5
- ember-cli-file-picker now depends on ember >=
1.11.1
Usage
{{#file-picker fileLoaded="fileLoaded"}} Drag here or click to upload a file{{/file-picker}}
Options
accept
default*
multiple
defaultfalse
selectOnClick
defaulttrue
dropzone
defaulttrue
preview
defaulttrue
progress
defaulttrue
hideFileInput
defaulttrue
readAs
defaultreadAsFile
readAsFile
readAsArrayBuffer
readAsBinaryString
readAsDataURL
readAsText
Actions
fileLoaded
ImplementfileLoaded
in your controller to handle the file.
Example:
// app/controllers/image.js; ObjectController;
Bindings
errors
removePreview
Validations
If you need to validate the files you can subclass the component and add a filesAreValid
method.
The method should return a falsy value to stop file handling.
// app/components/file-picker.js ;; ;
CSS
The addon provides the following classes to style the file-picker:
.file-picker(.single|multiple &.over)
.file-picker__preview
.file-picker__preview__image.single
.file-picker__preview__image.multiple
.file-picker__progress
.file-picker__preview__value
.file-picker__dropzone
.file-picker__input
Test helpers
ember-cli-file-picker exports a test helper for acceptance tests.
// tests/helpers/start-app.js; // tests/.jshintrc "predef": "uploadFile" // tests/acceptance/file-upload.js;; ; ;
Use with CarrierWave
// app/models/post.js; var attr = DSattr; Model;
// app/transforms/raw.js;; Transform;
// app/templates/application.hbs {{#file-picker accept=".jpg,.jpeg,.gif,.png" fileLoaded="fileLoaded" readAs="readAsDataURL"}}
// app/controllers/application.js; Controller;
params[:post][:image] = convert_to_upload(params[:post][:image]) @post = Post.create(params) end protected image_data = split_base64(image[:data]) temp_img_file = Tempfile.new("data_uri-upload") temp_img_file.binmode temp_img_file << Base64.decode64(image_data[:data]) temp_img_file.rewind ActionDispatch::Http::UploadedFile.new({ filename: image[:name], type: image[:type], tempfile: temp_img_file }) end if uri_str.match(%r{^data:(.*?);(.*?),(.*)$}) uri = Hash.new uri[:type] = $1 # "image/gif" uri[:encoder] = $2 # "base64" uri[:data] = $3 # data string uri[:extension] = $1.split('/')[1] # "gif" return uri end endend
Running
ember server
- Visit your app at http://localhost:4200.
Running Tests
ember test
ember test --server
Building
ember build
For more information on using ember-cli, visit http://www.ember-cli.com/.
Publishing
ember releasenpm publish