rails中的send_file不会下载到浏览器 - 而是直接在浏览器窗口中加载

时间:2017-08-04 15:02:20

标签: ruby-on-rails ruby erb

我的.erb中有以下内容:

<%= link_to 'Download PDF', planners_download_pdf_url %>

我有以下方法回应:

  def download_pdf
  send_file(
     "#{Rails.root}/Thing/ex/example.xls",
filename: "mything.xls",
type: "application/xls"
  )
  end

我的路线有:

get '/planners/download_pdf'

当我点击我的链接时,会调用该方法。我的问题是没有下载到浏览器。相反,它采用我在浏览器中打开的选项卡并将文件转储为HTML。就像......你见过在记事本中打开的Excel文件吗?它做到了。见下文:

Garbage

如何下​​载该文件?

1 个答案:

答案 0 :(得分:6)

您需要在From bc78aa61cfbddaa27dee275f639ba40de6981b17 Mon Sep 17 00:00:00 2001 From: George V. Kouryachy (Fr. Br. George) <george@> Date: Fri, 4 Aug 2017 18:37:33 +0300 Subject: [PATCH] parse_displayname: use FamilyWild for *:0 --- xauth/gethost.c | 4 ++++ xauth/parsedpy.c | 4 ++++ 2 files changed, 8 insertions(+), 0 deletions(-) diff --git a/xauth/gethost.c b/xauth/gethost.c index 8cb58c5..598ac48 100644 --- a/xauth/gethost.c +++ b/xauth/gethost.c @@ -180,6 +180,10 @@ struct addrlist *get_address_info ( * information to be copied and set len to the number of bytes. */ switch (family) { + case FamilyWild: /* was :0 */ + src = "\xff\xff"; + len = strlen(src); + break; case FamilyLocal: /* hostname/unix:0 */ /* handle unix:0 and :0 specially */ if (prefix == 0 && (strncmp (fulldpyname, "unix:", 5) == 0 || diff --git a/xauth/parsedpy.c b/xauth/parsedpy.c index 97988d3..6c98339 100644 --- a/xauth/parsedpy.c +++ b/xauth/parsedpy.c @@ -141,6 +141,10 @@ parse_displayname (const char *displayname, family = FamilyInternet; } #endif + } else if (len == 1 && *displayname == '*') { + /* ALT: wildcard cookie */ + host = copystring("*", 1); + family = FamilyWild; } else if (!dnet && (*displayname == '[') && (*(ptr - 1) == ']')) { /* Allow RFC2732-like [<IPv6NumericAddress>]:display syntax */ family = FamilyInternet6; -- 1.7.3.3 选项哈希中添加disposition: 'attachment'(或者至少确保您没有send_file设置,因为disposition: 'inline'应该是默认设置。

如果这不是问题,如果您正在使用Turbolinks,如other answers中所述,您需要{link {3}}在链接元素中设置'attachment'(例如:data-turbolinks="false"data: {turbolinks: false}代码帮助器中。

最后,如果这不起作用,可以尝试以下其他一些事项:

  • link_to设置为type,即XLS文件的有效MIME类型。
  • 直接在链接标记上设置'application/vnd.ms-excel' disable Turbolinks on the link(例如:download="mything.xls"代码帮助中的download: 'mything.xls'