关于创建下载链接的问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该在代码中读取该文件并将其按原样输出到客户端。
在其之前发送相关标头:
为什么在同一个地方进行两个重定向是不好的,这就像试图同时朝两个方向驾驶汽车......
更好
使用而不是
You should read the file in your code and output it as-is to the client.
Send before it the relevant headers:
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
instead of
好像没有人提到过 X-Sendfile。
正确下载文件
Seems like no one has mentioned X-Sendfile.
File Downloads Done Right
我尝试了下面的代码,只是一个示例 exe 文件,但代码看起来像是在读取超过 500 MB 的整个文件。 我希望它能给我保存 exe 文件的选项,谢谢!
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!
要通过控制器代理文件,您可以使用 open-uri。
在environment.rb中
在你的控制器中
编辑:如果你不想代理该文件,则必须redirect_to(url)。 如果您收到有关重定向的消息被调用两次,请记住,您不能从同一个操作同时进行渲染和重定向。 如果您需要加载视图,然后开始下载,请使用两个操作:
在您的视图 (present_download.html.erb) 中:
To proxy the file through your controller, you can use open-uri.
In environment.rb
In your controller
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:
And in your view (present_download.html.erb):