Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:15:44+00:00 2026-06-07T15:15:44+00:00

Some time ago I wrote a question regarding the use of temporary files within

  • 0

Some time ago I wrote a question regarding the use of temporary files within a rails app. On thar particular case, I decided to use tempfile

This causes a problem if I also want to use the x-sendfile directive (as a parameter in Rails 2, or as a configuration option in Rails 3) so that the file sending is handled by my web server directly, not my rails app.

So I thought about doing something like this:

require 'tempfile'

def foo()
  # creates a temporary file in tmp/
  Tempfile.open('prefix', "#{Rails.root}/tmp") do |f|
    f.print('a temp message')
    f.flush
    send_file(f.path, :x_sendfile => true) # send_file f.path in rails 3
  end
end

This setup has one issue: the file is deleted before being sent!

On one hand, tempfile will delete the file as soon as the Tempfile.open block is over. On the other, x-sendfile makes the send_file call asynchronous – it returns very quickly, so the server hardly has time to send the file.

My best possible solution right now involves using non-temporary files (File instead of Tempfile), and then a cron task that erases the temp folder periodically. This is a bit inelegant since:

  • I have to use my own tempfile naming scheme
  • Files stay on the tmp folder longer than they are needed.

Is there a better setup? Or, is there at least a “success” callback on the asynchronous send_file, so I can erase f when it’s done?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:15:46+00:00Added an answer on June 7, 2026 at 3:15 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some time ago, I wrote a small app, which needs to create outbound phone
I wrote an iPhone app some time ago that creates sound programatically. It uses
Some time ago, I wrote some code to decide which method of updating mutable
Some time ago I created this LAMP based web. At the time I wrote
I've got a function I wrote quite some time ago that works fine, but
Some time ago I was asked the strange question how would I implement map
Some time ago i posted a question related to a WriteableBitmap memory leak, and
Some time ago, i wrote some bash scripts for my school. I thought it
I wrote a program some time ago (Mac OS X, C++, SDL, FMOD) and
So I'm working with some older code I wrote some time ago. I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.