I have Ember App (built with Ember-CLI). On the backend I have Rails application which handles requests. Lets say that my API has following routes:
/model
— returns list of models
/model/1
— returns model
/model/1/download
— returns file
How can I force Ember app to start file download (using /download
path) and how can I construct link for file download from within Ember app?
Why don't you just return the URL path for the download in your model for "/model/1". Then you can easily create links in your handlebars templates like so:
<a href="{{model.download}}" target="_blank">Download File</a>
Content-disposition=attachment; filename=some.file.name
download
attribute;<a href="{{model.download}}" download>Download File</a>
Don't do any of these 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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With