-->

Rails - ActionDispatch::Http::UploadedFile in back

2019-06-17 23:44发布

问题:

I'm using a similar idea as in the importing csv and excel Railscast but as the standard code in that episode takes some time to process (uses ActiveRecord to create a new record for each row in the file) I'm getting timeouts on Heroku and would like to move the import process to a background job.

I have been unsuccessful at sending the file variable (which is of type ActionDispatch::Http::UploadedFile) to the job so instead I sent individual variables of the file.original_filename and file.path

The job fails with the error file /var/folders/q3/xn0bp7yd2m56_4lbq0069jj80000gn/T/RackMultipart20150319-72431-1a4pnja.xlsx does not exist which I assume is happening because the file has already been deleted before the job begins as:

Uploaded files are temporary files whose lifespan is one request. When the object is finalized Ruby unlinks the file, so there is no need to clean them with a separate maintenance task.

ActionDispatch::Http::UploadedFile

Can a file uploaded with ActionDispatch::Http::UploadedFile not be used in background jobs?

I am using Rails 4.2, ActiveJob and Resque

回答1:

No, the uploaded file cannot be used in the background job. What you need to do is save the uploaded file to a more permanent location for your background job to process.

Your controller will need to something like:

file_path_to_save_to = '/path/to/file'
File.write(file_path_to_save_to, params[:uploaded_file].read)
BackgroundJob.perform_later file_path_to_save_to