Download files using the Ember app

I have an Ember application (built with Ember-CLI). On the backend, I have a Rails application that processes requests. Lets say my API has the following routes:

/model - returns a list of models

/model/1 - returns the model

/model/1/download - returns a file

How to get the Ember application to start a file download (using the /download path) and how can I create a link to download files from an Ember application?

+5
source share
3 answers
  • If you are going to get to the endpoint and return the contents of the file, I believe that you will need to set the Content Disposition header. Read more here: Download using js or request

Content-disposition=attachment; filename=some.file.name

  1. If you are directly linking to a file, you can use the HTML5 download attribute;

<a href="{{model.download}}" download>Download File</a>

+3
source

Why don't you just return the download URL in your model for "/ model / 1". Then you can easily create links in your pen templates like this:

<a href="{{model.download}}" target="_blank">Download File</a>

+2
source

Do not do this manually, keep it simple and use ember-cli-file-saver !

 // models/invoice.js export default Model.extend({ invoiceNumber: attr('number'), download: memberAction({ path: 'download', type: 'GET', ajaxOptions: { arraybuffer: true } }) }); 
 // in a component invoiceModel .download() .then((pdfContent) => this.saveFileAs('invoice.pdf', pdfContent, 'application/pdf')); 
+1
source

Source: https://habr.com/ru/post/1207222/


All Articles