In Ruby on Rails, After send_file method delete the file from server

后端 未结 3 768
执笔经年
执笔经年 2020-12-05 02:16

I am using following code for sending the file in Rails.

if File.exist?(file_path)
  send_file(file_path, type: \'text/excel\') 
  File.delete(file_path)
end
         


        
3条回答
  •  猫巷女王i
    2020-12-05 02:53

    This works for me! With send_data you can delete file before send.

    file = File.open(Rails.root.join('public', 'uploads', filename), "rb")
    contents = file.read
    file.close
    
    File.delete(filepath) if File.exist?(filepath)
    
    send_data(contents, :filename => filename)
    

提交回复
热议问题