且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails 使用 send_file 发送多个文件

更新时间:2023-11-24 12:34:10

send_file 告诉控制器它应该通过发送文件来响应浏览器的请求.-- 与渲染视图、发送 JSON 等相反.

send_file tells the controller that it should respond to the browser's request by sending a file. -- As opposed to rendering a view, sending JSON, etc.

通常情况下,您在 HTTP 中只发送一个响应.(我省略了对长轮询和其他深奥类型的响应的讨论.我还省略了目前普遍不支持的 HTTP 多部分响应.)

In common usage, you send exactly one response in HTTP. (I'm omitting discussion of long-polling and other esoteric types of responses. I'm also omitting HTTP multipart responses which are not generally supported at this time.)

既然你只能发送一个文件,那就算数吧!一个文件可以是多个文件的 zip,但用户需要解压缩它们.

Since you can only send one file, make it count! The one file can be a zip of a number of files, but then the user will need to unzip them.

另一种方法是在网页上显示多个下载链接,邀请用户依次下载以完成多次下载.

An alternative is to show multiple download links on the web page, inviting the user to download one after another to accomplish the multiple downloads.

以 UX(用户体验)为例:向自己发送一封包含多个附件的电子邮件.然后使用 GMail,看看他们如何呈现多个文件供您下载.

As an example UX (User Experience): Send an email to yourself with multiple attachments. Then use GMail and see how they present the multiple files for you to download.