Ruby发送_文件并使用FileUtils将其直接存储到本地文件夹

Ruby发送_文件并使用FileUtils将其直接存储到本地文件夹,ruby,sinatra,nokogiri,fileutils,Ruby,Sinatra,Nokogiri,Fileutils,我目前可以发送docx文件供用户下载,问题是如何保存从send\u file方法创建的下载文件,以存储到ruby应用程序的本地目录中? 下面是使用发送文件的代码: send_file rand_file, :type => 'docx', :filename => "#{@report.report_name}.docx" 在调用send_file之前保存该文件,然后将其作为引用 file = File.open(temp_file_path, 'w+') file.binmode

我目前可以发送docx文件供用户下载,问题是如何保存从
send\u file
方法创建的下载文件,以存储到ruby应用程序的本地目录中?

下面是使用
发送文件的代码:

send_file rand_file, :type => 'docx', :filename => "#{@report.report_name}.docx"

在调用send_file之前保存该文件,然后将其作为引用

file = File.open(temp_file_path, 'w+')
file.binmode
file.write(rand_file.read)
file.close
file = Tempfile.new('temp_file_path', 'tmp')
send_file file, ...

在调用send_file之前保存该文件,然后将其作为引用

file = File.open(temp_file_path, 'w+')
file.binmode
file.write(rand_file.read)
file.close
file = Tempfile.new('temp_file_path', 'tmp')
send_file file, ...

我终于解决了我的问题,使用@katafrakt和@rantingsonrails的技巧,在send_file命令之前使用FileUtils方法进行复制。下面是我如何做到这一点的代码

temp_file_path = "./document/#{@report.report_name}.docx"
FileUtils::copy_file(rand_file,temp_file_path)

我终于解决了我的问题,使用@katafrakt和@rantingsonrails的技巧,在send_file命令之前使用FileUtils方法进行复制。下面是我如何做到这一点的代码

temp_file_path = "./document/#{@report.report_name}.docx"
FileUtils::copy_file(rand_file,temp_file_path)

应用程序或用户的本地目录?服务器目录中的@SergioTulentsev由于文件已在服务器上,您只需将其复制(或移动)到所需位置(
FileUtils.cp
)即可“保存”文件。感谢@katafrakt提供的tipsdirectory本地目录,应用程序还是服务器目录中的用户?@SergioTulentsev由于服务器上已有该文件,您只需将其复制(或移动)到所需位置(
FileUtils.cp
)即可“保存”该文件。感谢@katafrakt提供您的提示,但我同意katafrakt关于使用(FileUtils.cp)的建议作为我问题的解决方案。谢谢你的提示,但我同意katafrakt关于使用(FileUtils.cp)作为我问题的解决方案的建议。