本文介绍了Rails - 后台作业中的 ActionDispatch::Http::UploadedFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的想法与导入 csv 和 excel Railscast 但由于该集中的标准代码需要一些时间来处理(使用 ActiveRecord 为文件中的每一行创建一个新记录)我在 Heroku 上超时并希望将导入过程移至后台作业.

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.

我未能成功将文件变量(类型为 ActionDispatch::Http::UploadedFile)发送到作业,因此我发送了 file.original_filename 和 file.path 的各个变量

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

作业失败,出现错误 file/var/folders/q3/xn0bp7yd2m56_4lbq0069jj80000gn/T/RackMultipart20150319-72431-1a4pnja.xlsx 不存在 我认为这已经发生了,因为文件已被删除在工作开始之前:

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:

上传的文件是临时文件,其生命周期为一个请求.当对象最终确定时,Ruby 会取消链接文件,因此无需使用单独的维护任务清理它们.

ActionDispatch::Http::UploadedFile

使用 ActionDispatch::Http::UploadedFile 上传的文件可以不用于后台作业吗?

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

我正在使用 Rails 4.2、ActiveJob 和 Resque

I am using Rails 4.2, ActiveJob and Resque

推荐答案

不行,上传的文件不能用于后台作业.您需要做的是将上传的文件保存到一个更永久的位置,以便您的后台作业进行处理.

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

这篇关于Rails - 后台作业中的 ActionDispatch::Http::UploadedFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:11