当CarrierWave处理时,临时文件消失

问题描述 投票:0回答:1

我创建了一个ActiveJob来处理我的载波上传。但是,当我上传多个图像时,第二个文件出现以下错误:

Errno::ENOENT (No such file or directory @ rb_sysopen - C:/Users/tdavi/AppData/Local/Temp/RackMultipart20180830-392-z2s2i.jpg)

这是我的控制器中的代码:

if @post.save
  files = params[:post_attachments].map { |p|
    {image: p['photo'][:image].tempfile.path, description: p['photo'][:decription]}
  }
  ProcessPhotosJob.perform_later(@post.id, files.to_json)
  format.html { render :waiting }
end

还有我的ActiveJob

require 'json'

class ProcessPhotosJob < ApplicationJob
  queue_as :default

  def perform(post_id, photos_json)
    post = Post.friendly.find(post_id)
    photos = JSON.parse photos_json

    photos.each do |p|
        src_file = File.new(p['image'])
        post.post_attachments.create!(:photo => src_file, :description => p[:description])
    end

    post.processed = true
    post.save
  end
end

当我只上传一个要上传的文件时,它可以正常工作。

ruby-on-rails carrierwave temporary-files rails-activejob
1个回答
1
投票

您不应该将Tempfile传递给排队的作业。

首先 - Ruby可以自动删除TempFiles(docsexplanation

如果您想上传文件并稍后处理(在后台),那么我建议您检查this question

© www.soinside.com 2019 - 2024. All rights reserved.