lundi 26 février 2018

Download files in ember: using Ember-cli-file-saver

It might be plain ignorance from my part but I have only managed to download a file generated by the api using the model method mention in the documentation. Using a component I am quite blind.

The specific question would be: where do I pass the mention arraybuffer:true to the application adapter or to a custom ajax request? Do you have a working example?

Here is a simple try using an ajax service:

import Component from '@ember/component';
import FileSaverMixin from 'ember-cli-file-saver/mixins/file-saver';
import { inject as service } from '@ember/service';

export default Component.extend(FileSaverMixin, {
  tagName: 'div',
  ajax: service(),
  store: service(),
  click() {
    this.get('ajax').request('/excel', {
      options: {
        arraybuffer: true
      }
      }
    ).then((content) => {
      console.log(content);
      this.saveFileAs(this.get('filename'), content, this.get('contentType'));
    }).catch((error) => {
      console.log(error);
    })
  }
});

And this is my adapter:

import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
import AdapterArrayBufferMixin from 'ember-cli-file-saver/mixins/adapter-arraybuffer-mixin';
import ENV from 'efac-front/config/environment';

export default DS.JSONAPIAdapter.extend(
  DataAdapterMixin,
  AdapterArrayBufferMixin,
  {
    authorizer: 'authorizer:token',
    namespace: 'api',
    host: ENV.host
  }
);

I keep getting an error of SyntaxError: Unexpected token P in JSON at position 0... because it is trying to interpret an array buffer or binary response as json data.

I very much appreciate any light you can throw here




Aucun commentaire:

Enregistrer un commentaire