我剛剛花了兩天的時間試圖做到這一點,並最終找到了答案。您需要對文件進行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
似乎是唯一的方法。我之前只是不想處理文件的移動和刪除。謝謝。 –