Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ember-cli-file-picker

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-cli-file-picker

An addon for ember-cli that provides a component to easily add a filepicker to your ember-cli app.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
731
increased by1.25%
Maintainers
1
Weekly downloads
 
Created
Source

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

  • npm install --save-dev ember-cli-uploader

Usage

{{#file-picker fileLoaded="fileLoaded"}}
	Drag here or click to upload a file
{{/file-picker}}

Options

  • accept default *
  • multiple default false
  • preview default true
  • dropzone default true
  • readAs default readAsFile Options: readAsFile, readAsArrayBuffer, readAsBinaryString, readAsDataURL, readAsText

Actions

  • fileLoaded Implement fileLoaded in your controller to handle the file.

Example:

// app/controllers/image.js
import Ember from 'ember';

export default Ember.ObjectController.extend({
	actions: {
		fileLoaded: function(file) {
			// readAs="readAsFile"
			console.log(file.name, file.type, file.size);
			// readAs="readAsArrayBuffer|readAsBinaryString|readAsDataURL|readAsText"
			console.log(file.filename, file.type, file.data, file.size);
		}
	}
});

Bindings

  • errors

CSS

The addon provides the following classes to style the file-picker:

  • .file-picker(.single|multiple)
    • .file-picker__preview
      • .file-picker__preview__image.single
      • .file-picker__preview__image.multiple
    • .file-picker__dropzone
    • .file-picker__input

Use with CarrierWave

// app/models/post.js
import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
	image: attr('raw')
});
// app/transforms/raw.js
import DS from 'ember-data';
import Ember from 'ember';

export default DS.Transform.extend({
	deserialize: function(serialized) {
		return Ember.isNone(serialized) ? {} : serialized;
	},

	serialize: function(deserialized) {
		return Ember.isNone(deserialized) ? {} : deserialized;
	}
});
// app/templates/application.hbs

{{#file-picker
  accept=".jpg,.jpeg,.gif,.png"
  fileLoaded="fileLoaded"
  readAs="readAsDataURL"
}}
// app/controllers/application.js
import Ember from 'ember';

export default Ember.Controller.extend({
	actions: {
		fileLoaded: function(file) {
			var post = this.get('store').createRecord('post', {
				image: file
			});
			post.save();
		}
	}
});
class PostsController < ApplicationController
	def create
		params[:post][:image] = convert_to_upload(params[:post][:image])
		@post = Post.create(params)
	end
	
	protected

	def convert_to_upload(image)
		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[:filename],
		  type: image[:type],
		  tempfile: temp_img_file
		})
	end

	def split_base64(uri_str)
		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
		else
			return nil
		end
	end
end

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/.

Keywords

FAQs

Package last updated on 29 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc