在Ruby on Rails中,send_data和send_file有什么区别?

时间:2023-02-06 00:10:00

Which one is best for streaming and file downloads?

哪种方式最适合流媒体和文件下载?

Please provide examples.

请提供例子。

2 个解决方案

#1


90  

send_data(_data_, options = {})
send_file(_path_, options = {}) 

Main difference here is that you pass DATA (binary code or whatever) with send_data or file PATH with send_file.

这里的主要区别是您使用send_data传递数据(二进制代码或其他)或使用send_file传递文件路径。

So you can generate some data and send it as an inline text or as an attachment without generating file on your server via send_data. Or you can send ready file with send_file

因此,您可以生成一些数据并将其作为内联文本或附件发送,而无需通过send_data在服务器上生成文件。或者您可以使用send_file发送准备好的文件。

data = "Hello World!"
send_data( data, :filename => "my_file.txt" )

Or

data = "Hello World!"
file = "my_file.txt"
File.open(file, "w"){ |f| f << data }
send_file( file )

For perfomance it is better to generate file once and then send it as many times as you want. So send_file will fit better.

对于性能,最好生成一次文件,然后根据需要多次发送。send_file会更适合。

For streaming, as far as I understand, both of this methods use the same bunch of options and settings, so you can use X-Send or whatever.

对于流,据我所知,这两种方法都使用相同的选项和设置,因此可以使用X-Send或其他方法。

UPD

乌利希期刊指南

send_data and save file:

send_data并保存文件:

data = "Hello World!"
file = "my_file.txt"
File.open(file, "w"){ |f| f << data }
send_data( data )

#2


13  

send_file may be faster than send_data

send_file可能比send_data快。

As fl00r mentioned, send_file takes a path, and send_data the data.

正如fl00r所提到的,send_file接受一个路径,并对数据进行send_data。

Therefore send_file is a subset of send_data, as you need a file on the filesystem: you could of course just read the file and use send_data on it. But send_file can be faster, so it is a performance / generality trade-off.

因此,send_file是send_data的一个子集,因为您需要在文件系统上有一个文件:您当然可以读取文件并在其上使用send_data。但是send_file可以更快,所以这是一个性能/通用性的权衡。

send_file can be faster because it can send the X-Sendfile header on Apache (X-Accel-Redirect on Nginx) instead of the file content, since it knows the path.

send_file可以更快,因为它可以在Apache上发送X-Sendfile头(Nginx上的X-Accel-Redirect),而不是文件内容,因为它知道路径。

This header is consumed by the reverse proxy (Apache or Nginx) which normally runs in front of Rails in a production setup.

这个头被反向代理(Apache或Nginx)使用,在生产设置中,反向代理通常在Rails前面运行。

If X-Sendfile is present on the response, the reverse proxy ignores most of the current response, and builds a new one that returns the file at the given path.

如果X-Sendfile出现在响应上,反向代理将忽略当前的大多数响应,并构建一个新的响应,在给定的路径上返回文件。

Client <---> Internet <---> Reverse proxy <---> Rails

This is much more efficient since the reverse proxy is highly specialized at serving static files, and can do it much faster than Rails (which does not send the file data if X-Sendfile will be sent).

这要高效得多,因为反向代理专门处理静态文件,并且可以比Rails(如果发送X-Sendfile, Rails不会发送文件数据)快得多。

The typical use case of send_file is when you want to control the access permission of static files: you cannot put them under /public or else they would get served before Rails has a chance to decide. This is discussed at: Protecting the content of public/ in a Rails app

send_file的典型用例是当您想要控制静态文件的访问权限时:您不能将它们置于/public或else中,否则它们将在Rails有机会决定之前得到服务。这在:保护公共/ Rails应用程序中的内容上进行了讨论

In order to use the X-Sendfile headers, you have to add:

为了使用X-Sendfile标头,您必须添加:

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

to confing/initializers/production.rb (not application.rb, since in development you don't have a proxy server and you want send_file to actually send the data).

conf /初始化/生产。rb(而不是应用程序。rb,因为在开发中没有代理服务器,您希望send_file实际发送数据)。

X-Sendfile is discussed on the Asset Pipeline Guide.

在资产管道指南中讨论了X-Sendfile。

#1


90  

send_data(_data_, options = {})
send_file(_path_, options = {}) 

Main difference here is that you pass DATA (binary code or whatever) with send_data or file PATH with send_file.

这里的主要区别是您使用send_data传递数据(二进制代码或其他)或使用send_file传递文件路径。

So you can generate some data and send it as an inline text or as an attachment without generating file on your server via send_data. Or you can send ready file with send_file

因此,您可以生成一些数据并将其作为内联文本或附件发送,而无需通过send_data在服务器上生成文件。或者您可以使用send_file发送准备好的文件。

data = "Hello World!"
send_data( data, :filename => "my_file.txt" )

Or

data = "Hello World!"
file = "my_file.txt"
File.open(file, "w"){ |f| f << data }
send_file( file )

For perfomance it is better to generate file once and then send it as many times as you want. So send_file will fit better.

对于性能,最好生成一次文件,然后根据需要多次发送。send_file会更适合。

For streaming, as far as I understand, both of this methods use the same bunch of options and settings, so you can use X-Send or whatever.

对于流,据我所知,这两种方法都使用相同的选项和设置,因此可以使用X-Send或其他方法。

UPD

乌利希期刊指南

send_data and save file:

send_data并保存文件:

data = "Hello World!"
file = "my_file.txt"
File.open(file, "w"){ |f| f << data }
send_data( data )

#2


13  

send_file may be faster than send_data

send_file可能比send_data快。

As fl00r mentioned, send_file takes a path, and send_data the data.

正如fl00r所提到的,send_file接受一个路径,并对数据进行send_data。

Therefore send_file is a subset of send_data, as you need a file on the filesystem: you could of course just read the file and use send_data on it. But send_file can be faster, so it is a performance / generality trade-off.

因此,send_file是send_data的一个子集,因为您需要在文件系统上有一个文件:您当然可以读取文件并在其上使用send_data。但是send_file可以更快,所以这是一个性能/通用性的权衡。

send_file can be faster because it can send the X-Sendfile header on Apache (X-Accel-Redirect on Nginx) instead of the file content, since it knows the path.

send_file可以更快,因为它可以在Apache上发送X-Sendfile头(Nginx上的X-Accel-Redirect),而不是文件内容,因为它知道路径。

This header is consumed by the reverse proxy (Apache or Nginx) which normally runs in front of Rails in a production setup.

这个头被反向代理(Apache或Nginx)使用,在生产设置中,反向代理通常在Rails前面运行。

If X-Sendfile is present on the response, the reverse proxy ignores most of the current response, and builds a new one that returns the file at the given path.

如果X-Sendfile出现在响应上,反向代理将忽略当前的大多数响应,并构建一个新的响应,在给定的路径上返回文件。

Client <---> Internet <---> Reverse proxy <---> Rails

This is much more efficient since the reverse proxy is highly specialized at serving static files, and can do it much faster than Rails (which does not send the file data if X-Sendfile will be sent).

这要高效得多,因为反向代理专门处理静态文件,并且可以比Rails(如果发送X-Sendfile, Rails不会发送文件数据)快得多。

The typical use case of send_file is when you want to control the access permission of static files: you cannot put them under /public or else they would get served before Rails has a chance to decide. This is discussed at: Protecting the content of public/ in a Rails app

send_file的典型用例是当您想要控制静态文件的访问权限时:您不能将它们置于/public或else中,否则它们将在Rails有机会决定之前得到服务。这在:保护公共/ Rails应用程序中的内容上进行了讨论

In order to use the X-Sendfile headers, you have to add:

为了使用X-Sendfile标头,您必须添加:

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

to confing/initializers/production.rb (not application.rb, since in development you don't have a proxy server and you want send_file to actually send the data).

conf /初始化/生产。rb(而不是应用程序。rb,因为在开发中没有代理服务器,您希望send_file实际发送数据)。

X-Sendfile is discussed on the Asset Pipeline Guide.

在资产管道指南中讨论了X-Sendfile。