关于创建下载链接的问题

发布于 2024-07-15 05:33:03 字数 328 浏览 7 评论 0原文

我是 Rails 新手,正在开发一个项目,用户登录后 他们可以单击链接来下载 exe 文件(例如 文件位于 http://test.com/test.exe)。 我想保留exe的链接 文件隐藏。 实施此步骤的最佳方法是什么?

我正在考虑使用重定向到 url 但我一直在 错误提示我无法在函数中使用两个重定向。

另外,我正在考虑使用 NET:HTTP 来使用 http 请求,但我没有 想法如何实现这一点。

谢谢!!

I'm new to Rails and working on a project where after the user logs in
they can click on a link to download an exe file ( say for example the
file is at http://test.com/test.exe). I want to keep the link to the exe
file hidden. What are the best ways to implement this step.

I was thinking about using the redirect to url but I have been getting
an error saying that I cannot use two redirect's in a function.

Also, I was thinking of using NET:HTTP to use http request but I have no
idea how to implement this.

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南七夏 2024-07-22 05:33:03

您应该在代码中读取该文件并将其按原样输出到客户端。
在其之前发送相关标头:

Content-type: application/octet-stream  
Content-Disposition: inline; filename=[file name you want the user to see]  
Content-length: [file size]  

为什么在同一个地方进行两个重定向是不好的,这就像试图同时朝两个方向驾驶汽车......

更好

Content-Type: application/force-download

使用而不是

Content-type: application/octet-stream

You should read the file in your code and output it as-is to the client.
Send before it the relevant headers:

Content-type: application/octet-stream  
Content-Disposition: inline; filename=[file name you want the user to see]  
Content-length: [file size]  

Why doing two redirects in same place is bad, it is like trying to steer a car in two directions at once...

It might be better to use

Content-Type: application/force-download

instead of

Content-type: application/octet-stream
忱杏 2024-07-22 05:33:03

好像没有人提到过 X-Sendfile。

正确下载文件

Seems like no one has mentioned X-Sendfile.

File Downloads Done Right

你的心境我的脸 2024-07-22 05:33:03

我尝试了下面的代码,只是一个示例 exe 文件,但代码看起来像是在读取超过 500 MB 的整个文件。 我希望它能给我保存 exe 文件的选项,谢谢!

  def download
    send_data(open('http://mirrors.gigenet.com/ubuntu/intrepid/ubuntu-8.10-desktop-i386.iso').read,
    :filename => 'ubuntu-8.10-desktop-i386.iso' ,
    :type => 'application/force-download',
    :disposition => 'attachment')
  end

I tried the below code, just an example exe file but the code looks like is reading the whole file which is over 500 MB. I want it to just give me the option to save the exe file Thanks!

  def download
    send_data(open('http://mirrors.gigenet.com/ubuntu/intrepid/ubuntu-8.10-desktop-i386.iso').read,
    :filename => 'ubuntu-8.10-desktop-i386.iso' ,
    :type => 'application/force-download',
    :disposition => 'attachment')
  end
_蜘蛛 2024-07-22 05:33:03

要通过控制器代理文件,您可以使用 open-uri。

在environment.rb中

require 'open-uri'

在你的控制器中

def download
  send_data(open(params[:url]).read,
    :filename => 'somefilename',
    :disposition => 'attachment'
  )
end

编辑:如果你不想代理该文件,则必须redirect_to(url)。 如果您收到有关重定向的消息被调用两次,请记住,您不能从同一个操作同时进行渲染和重定向。 如果您需要加载视图,然后开始下载,请使用两个操作:

def present_download
  @download = Download.find(params[:id])
  # implicitly calls render :action => :present_download
end

def download_file
  @download = Download.find(params[:id])
  respond_to do |format|
    format.html { redirect_to(@download.url) }
  end
end

在您的视图 (present_download.html.erb) 中:

<html>
  <head>
    <meta http-equiv="refresh" content="1;url=<%= url_for :action => :download_file -%>" />
  </head>
  <body>
    Your download will automatically start…
…

To proxy the file through your controller, you can use open-uri.

In environment.rb

require 'open-uri'

In your controller

def download
  send_data(open(params[:url]).read,
    :filename => 'somefilename',
    :disposition => 'attachment'
  )
end

EDIT: If you don't want to proxy the file, you have to redirect_to(url). If you're getting the message about redirect called twice, then keep in mind that you cannot both render and redirect from the same action. If you need to load a view, and then start a download, use two actions:

def present_download
  @download = Download.find(params[:id])
  # implicitly calls render :action => :present_download
end

def download_file
  @download = Download.find(params[:id])
  respond_to do |format|
    format.html { redirect_to(@download.url) }
  end
end

And in your view (present_download.html.erb):

<html>
  <head>
    <meta http-equiv="refresh" content="1;url=<%= url_for :action => :download_file -%>" />
  </head>
  <body>
    Your download will automatically start…
…
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文