How: File Upload with ember.js

15,275

Solution 1

See my answer from another thread

<input
  multiple="true"
  onchange={{action "upload"}}
  accept="image/png,image/jpeg,application/pdf"
  type="file"
/>

actions: {
  upload: function(event) {
    const reader = new FileReader();
    const file = event.target.files[0];
    let imageData;

    // Note: reading file is async
    reader.onload = () => {
      imageData = reader.result;
      this.set(data.image', imageData);

      // additional logics as you wish
    };

    if (file) {
      reader.readAsDataURL(file);
    }
  }
}

It just works.

Solution 2

If you read the answers in the link below, you will understand how to do file upload and save to server with emberjs:

File upload with Ember data

In the answer provided by 'Toran Billups' in the link above, the lines below, which I copied from his answer, do the saving to server:

var person = PersonApp.Person.createRecord({username: 'heyo', attachment: fileToUpload});

self.get('controller.target').get('store').commit()
Share:
15,275
fabian
Author by

fabian

Updated on June 25, 2022

Comments

  • fabian
    fabian almost 2 years

    I was wondering how to do a real file upload (save file to server) with ember.js

    Are there any good examples?