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

时间:2018-01-15 20:05:00

标签: ruby sinatra nokogiri fileutils

我目前能够发送一个docx文件供用户下载,问题是如何保存从send_file方法创建的下载文件,以存储到ruby应用程序的本地目录中吗

以下是使用send_file的代码:

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

2 个答案:

答案 0 :(得分:1)

在调用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, ...

答案 1 :(得分:1)

我终于通过在send_file命令之前使用FileUtils复制方法,使用来自@katafrakt和@rantingsonrails的提示来解决我的问题。以下是我如何做到的代码。

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