javascript 内容处置:附件不触发下载对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26737883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:25:18  来源:igfitidea点击:

Content-Disposition:attachment not triggering download dialog

javascriptnode.jscsvexpresscontent-disposition

提问by Yaron Schwimmer

I've encountered some unexpected behavior when trying to create a file download functionality on my NodeJS server. I have a REST (express) API that calls for some export data function, which creates a CSV file on the server and uses res.download('path/to/file')to trigger the download. Response headers include

尝试在我的 NodeJS 服务器上创建文件下载功能时,我遇到了一些意外行为。我有一个 REST (express) API,它调用一些导出数据函数,它在服务器上创建一个 CSV 文件并用于res.download('path/to/file')触发下载。响应头包括

Content-Disposition:attachment; filename="indicators.csv"
Content-Length:30125
Content-Type:text/csv; charset=UTF-8

so everything seems to be in order.

所以一切似乎都井井有条。

The thing is, I get the response from the server as plain text. The response has all the data the CSV file contains, but does not trigger the browser's file download dialog like I intended. I tried both on Chrome and FF. The problem persists in both.

问题是,我从服务器获得纯文本响应。响应包含 CSV 文件包含的所有数据,但不会像我预期的那样触发浏览器的文件下载对话框。我在 Chrome 和 FF 上都试过。问题仍然存在于两者中。

Any ideas?

有任何想法吗?

Update

更新

I managed to make it work by creating a dummy form, and using its submit action to make my AJAX call. But it's an ugly hack, and I'm still looking for a more elegant solution.

我设法通过创建一个虚拟表单并使用其提交操作进行 AJAX 调用来使其工作。但这是一个丑陋的黑客,我仍在寻找更优雅的解决方案。

回答by Roman Pletnev

Headers are not the issue. The issue is that you are querying the download url via an ajaxcall, which will not invoke the browser download dialog. Your options boil down to the following:

标题不是问题。问题是您通过ajax调用查询下载 url ,这不会调用浏览器下载对话框。您的选择归结为以下几点:

  1. Use a form that is submitted to your download url. Instead of having a visible form a user has to interact with, create a form with JavaScriptand submit it programmatically by calling form.submit- Handle file download from ajax post

  2. Point window.locationto the download url. You can do this in the current window - download file using an ajax request, or in a new one - res.download() not working in my case

  1. 使用提交到您的下载 url 的表单。与其拥有一个用户必须与之交互的可见表单,不如创建一个表单JavaScript并通过调用以编程方式提交它form.submit-处理从 ajax 发布的文件下载

  2. 指向window.location下载地址。您可以在当前窗口中执行此操作 -使用 ajax 请求下载文件,或在新窗口中执行此操作- res.download() 在我的情况下不起作用

回答by MQ87

You can try to use a different content-type, so that won't be open as a text file on the browser:

您可以尝试使用不同的内容类型,这样它就不会在浏览器上作为文本文件打开:

Content-Type:application/ms-excel; charset=UTF-8

Another alternative could be to use application/octet-stream as mime-type to define it as a downloadable file without a better description.

另一种选择是使用 application/octet-stream 作为 mime-type 将其定义为可下载的文件,而没有更好的描述。

Content-Type:application/octet-stream; charset=UTF-8

回答by Pawel Zieminski

The question looks similar to this one

这个问题看起来类似于这个

Show 'Save as' dialog box while downloading file from an Iframe through PHP

通过 PHP 从 Iframe 下载文件时显示“另存为”对话框

The basic idea is the second option that Roman described above, but this one's using an iframe to achieve it.

基本思想是 Roman 上面描述的第二个选项,但是这个是使用 iframe 来实现的。