【发布时间】:2014-06-22 13:04:39
【问题描述】:
我有一个链接,我想添加到我的 javascript (Marionette/Backbone) 单页应用程序中,该应用程序将通过浏览器的文件保存将 Excel 文件下载到用户的本地驱动器。一个典型的 HTTP 请求是:
GET /api/v1/objects/?format=xls HTTP/1.1
Authorization: ApiKey username:apikey
Host: api.example.com
Connection: close
User-Agent: Paw 2.0.5 (Macintosh; Mac OS X 10.9.2; en_US)
Content-Length: 0
这会导致以下典型响应:
HTTP/1.1 200 OK
Server: gunicorn/18.0
Date: Tue, 06 May 2014 03:09:02 GMT
Connection: close
Transfer-Encoding: chunked
Vary: Accept
Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename="filename.xls"
Cache-Control: no-cache
<<CONTENT HERE>>>
我想用一个按钮样式的简单锚元素来实现这一点,因为这会调用浏览器的文件存储机制。类似于:
<a href="/api/v1/objects/?format=xls" class="btn btn-primary pull-right">Download to Excel file</a>
我不清楚在通过锚链接执行此操作时如何获取授权标头 - 或者我只是没有考虑并且有更好的方法。
我的后端是一个使用 Tastypie 的 Django Web 应用程序。
【问题讨论】:
标签: javascript html http backbone.js tastypie