In Ruby on Rails, After Send_File Method Delete the File from Server

Delete folder after send_file in Rails

Are you sure the send_file is not still sending the file when you are removing the dir, it may be asynchronous if it uses X-SendFile? That would cause an error when trying to remove the dir.

So you should probably be queuing this delete action, or doing it with a sweeper later, rather than trying to do it straight after sending the file to streaming.

I'm not completely clear on which file you are sending, so it would be useful to include an actual example of file path, and file type, and how it is created in your question.

Possible help with debugging:

Log in and monitor the folder while you perform the following actions:

  • Write out a very large file (> 60MB say), and check there is no invisible file created during your file creation process - I'm not clear on which file you are actually sending
  • Set up a large file transfer on a slow connection, and watch for the creation and possibly growing of this file (it might be related to compressing the file served on the fly for example).

Given that sendfile may still be sending (for large files) via the web server (x-send-file is now default) when you try to delete, I'd try looking into delayed solutions.

Possible solutions:

  • Use send_data rather than send_file (if files are small)
  • Schedule the deletion of the folder for later with something like delayed_job
  • Set up a sweeper which removes the folders at the end of each day

How can I delete a file in Sinatra after it has been sent via send_file?

Unfortunately there is no any callbacks when you use send_file. Common solution here is to use cron tasks to clean temp files

clean /tmp when send_file is over

I use send_data instead of send_file, then I delete the file. send_data will block until the data is sent, allowing File.delete request to succeed.

 file = temp.path
File.open(file, 'r') do |f|
send_data f.read.force_encoding('BINARY'), :filename => filename, :type => "application/pdf", :disposition => "attachment"
end
File.delete(file)

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

rails - x-sendfile + temporary files

Given that Rails3 uses x-sendfile when it is available, and there is no way to deactivate it, you just can't use send_file with a library such as TempFile. The best option is the one I mentioned in the question: use a regular File, and have a cron task that removes old temp files periodically.

EDIT: The removal of unused files has now been easier to deal with with the maid gem:

https://github.com/benjaminoakes/maid

send_file just sends an empty file

Problem saved, but I don't know why

File.open(file_path, 'r') do |f|
send_data f.read, :type => "text/xml", :filename => "10.xml"
end

send_data is working... but send_file not!

What happend after send_file in Rails controller?

The send_file is a render itself. You could use the approach proposed in this question:

Rails: send_file never renders page or DoubleRender error

Basically your download link will send to a "success" view, from which you call the download file method automatically.

send_file return value

No, there is no way to confirm download completion or success with send_file. From this question: Can we find out when a Paperclip download is complete? :

send_file creates the file and then passes a special header to tell
the webserver telling it what to send. Rails doesn't actually send the
file at all, it sets this header which tells the webserver to send the
file but then returns immediately, and moves on to serve another
request. To be able to track if the download completes you'd have to
occupy your Rails application process sending the file and block until
the user downloads it, instead of leaving that to the webserver (which
is what its designed to do). This is super inefficient.

You may be able to do something using cookies and JavaScript on the client.
See this question: Rails File Download And View Update - Howto?



Related Topics



Leave a reply



Submit