sirvine / ember-cli-form-data

Ember CLI addon that adds FormData file uploads to Ember Data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ember-cli-form-data

This Ember-CLI addon adds file uploads through FormData to the Ember Data

Install

npm install ember-cli-form-data --save-dev

Usage

Add a file field on the model

// models/post.js

export default DS.Model.extend({
  attachment: DS.attr('file'),
  ...
});

Add the FormDataMixin to your post adapter. Run ember g adapter post if you don't have the adapter.

// adapters/post.js

import FormDataAdapterMixin from 'ember-cli-form-data/mixins/form-data-adapter';

export default ApplicationAdapter.extend(FormDataAdapterMixin, {
  // Adapter code
});

Then you can use an <input type='file' id='file-field'/> to send the attachment:

var file = document.getElementById('file-field').files[0];
model.set('attachment', file);
model.save();

This will send the attachment and all other attributes as a FormData object.

Thanks

This addon was inspired by Matt Beedle's blog post http://blog.mattbeedle.name/posts/file-uploads-in-ember-data/

About

Ember CLI addon that adds FormData file uploads to Ember Data

License:MIT License


Languages

Language:JavaScript 76.8%Language:HTML 22.2%Language:Handlebars 0.6%Language:CSS 0.4%