拦截文件下载请求并将其发送到 Rails 应用程序?
我想制作一个 Rails 应用程序,为通过链接或输入文件的 url 请求的任何文件创建一个漂亮的下载页面。有没有办法拦截 Apache 或其他地方的文件请求并将其发送到应用程序,以便它可以生成页面?
我也不想在重定向到应用程序时更改 url,但这并不重要。
总结一下,请转:http://files.spherecat1.com/stuff.txt,进入此:
http://files.spherecat1.com/download-page-mockup.png
(图像仅用于说明目的,可能无法准确描述最终产品。向所有内容添加免责声明很有趣。)
I would like to make a Rails app to create a pretty download page for any file requested either through a link or by typing the url of the file. Is there a way to intercept the request for a file in Apache or elsewhere and send it to the app so it can generate the page?
I'd also prefer not to change the url when redirecting to the app, but it doesn't really matter either way.
So to wrap up, turn this: http://files.spherecat1.com/stuff.txt, into this:
http://files.spherecat1.com/download-page-mockup.png
(Image for illustrative purposes only and may not accurately depict final product. It's fun to add disclaimers to everything.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是整个 Rails 应用程序就是为了这个目的吗?看起来有点矫枉过正 - 你可以使用Sinatra,一条路线,以及他们的send_file 功能更加轻松。 它还可以通过 Passenger 部署在 Apache 上。
如果您有控制权在这件事上,并且您只计划以这种方式而不是任何其他方式提供下载,您可能应该将文件存储在某个非公共目录中,而不是允许您的 Rails/Sinatra 应用程序访问文件并将文件推送给用户。 (同样,使用 send_file,这很简单。)为什么要放置文件在您的网络根目录中,如果您的用户永远不会以这种方式访问它?
Do you mean an entire Rails app just for that? Seems like overkill - you can use Sinatra, a single route, and their send_file function with much greater ease. It also deploys on Apache with Passenger.
If you have control over this thing, and you only plan to offer downloads in this way and not in any other, you should probably store the files in some non-public directory and instead allow your Rails/Sinatra app to access and push the files to the user. (Again, with send_file, that's easy.) Why put a file in your web root if your users will never access it that way?