我刚刚花了两天的时间试图做到这一点,并最终找到了答案。您需要对文件进行Base64编码,以便将其序列化为json。然后,你需要它的工人进行解码,并创建一个新的
ActionDispatch::Http::UploadedFile
下面是如何编码,并传递给resque:
// You only need to encode the actual file, everything else in the
// ActionDispatch::Http::UploadedFile object is just string or a hash of strings
file = params[:file] // Your ActionDispatch::Http::UploadedFile object
file.tempfile.binmode
file.tempfile = Base64.encode64(file.tempfile.read)
Resque.enqueue(QueueWorker, params)
,这里是如何解码和内转换回对象你的工人
class QueueWorker
@queue = :main_queue
def self.perform(params)
file = params['file']
tempfile = Tempfile.new('file')
tempfile.binmode
tempfile.write(Base64.decode64(file['tempfile']))
// Now that the file is decoded you need to build a new
// ActionDispatch::Http::UploadedFile with the decoded tempfile and the other
// attritubes you passed in.
file = ActionDispatch::Http::UploadedFile.new(tempfile: tempfile, filename: file['original_filename'], type: file['content_type'], head: file['headers'])
// This object is now the same as the one in your controller in params[:file]
end
end
似乎是唯一的方法。我之前只是不想处理文件的移动和删除。谢谢。 –