【发布时间】:2017-10-25 08:54:27
【问题描述】:
我正在调用一个 API 以使用 fetch API 从服务器下载 excel 文件,但它并没有强制浏览器下载,下面是我的标题响应:
HTTP/1.1 200 OK Content-Length: 168667
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Server: Microsoft-IIS/8.5
Content-Disposition: attachment; filename=test.xlsx
Access-Control-Allow-Origin: http://localhost:9000
Access-Control-Allow-Credentials: true
Access-Control-Request-Method: POST,GET,PUT,DELETE,OPTIONS
Access-Control-Allow-Headers: X-Requested-With,Accept,Content-Type,Origin
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Wed, 24 May 2017 20:18:04 GMT
在我用来调用 API 的代码下方:
this.httpClient.fetch(url, {
method: 'POST',
body: JSON.stringify(object),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
【问题讨论】:
-
您可能想使用stackoverflow.com/posts/44168090/edit 更新/编辑您的问题,以添加您用于尝试下载文件的代码的 sn-p
-
你是如何“调用 API”的?
-
this.httpClient.fetch(url, { method: 'POST', body: JSON.stringify(object), headers: { 'Accept': 'application/json', 'Content-Type' : '应用程序/json' } })
-
我发现使用blob方法下载但是否有其他方法强制下载?
-
作为一项安全功能 - JavaScript 无法使用 AJAX 开始下载到您的计算机 - 因为它无权将文件写入您的计算机。
标签: download aurelia fetch-api