【发布时间】:2014-11-19 15:27:24
【问题描述】:
我有 Ember 应用程序(使用 Ember-CLI 构建)。在后端,我有处理请求的 Rails 应用程序。假设我的 API 有以下路由:
/model — 返回模型列表
/model/1 — 返回模型
/model/1/download — 返回文件
如何强制 Ember 应用开始文件下载(使用 /download 路径)以及如何在 Ember 应用中构建文件下载链接?
【问题讨论】:
标签: ember.js
我有 Ember 应用程序(使用 Ember-CLI 构建)。在后端,我有处理请求的 Rails 应用程序。假设我的 API 有以下路由:
/model — 返回模型列表
/model/1 — 返回模型
/model/1/download — 返回文件
如何强制 Ember 应用开始文件下载(使用 /download 路径)以及如何在 Ember 应用中构建文件下载链接?
【问题讨论】:
标签: ember.js
您为什么不直接在“/model/1”的模型中返回下载的 URL 路径。然后,您可以轻松地在车把模板中创建链接,如下所示:
<a href="{{model.download}}" target="_blank">Download File</a>
【讨论】:
Content-disposition=attachment; filename=some.file.name
download 属性;<a href="{{model.download}}" download>Download File</a>
【讨论】:
不要手动执行任何这些操作,保持简单并使用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'));
【讨论】: