2

我正在尝试使用rails回形针gem 上传一个 zip 文件,到目前为止它工作正常。但是下载完成后,我想解压这个文件并对里面的文件做一些事情。

当我尝试解压缩文件时出现问题,因为它不在它自己的路径中,可能它正在被复制但尚未完成。(而且我在本地主机中,在线模式下更糟)。

所以,我需要某种事件/触发器来知道文件何时完成上传以开始解压缩。同时,显示某种界面。控制器的代码如下:

# POST /components
# POST /components.json
def create
  @component = Component.new(params[:component])

  file = @component.folder

  Zip::ZipFile.open(file.path) do |zipfile|   # <-- The error comes here
    zipfile.each do |file|
      # do something with file
    end
  end


  respond_to do |format|
    if @component.save
      format.html { redirect_to @component, notice: 'Component was successfully created.' }
      format.json { render json: @component, status: :created, location: @component }
    else
      format.html { render action: "new" }
      format.json { render json: @component.errors, status: :unprocessable_entity }
    end
  end
end
4

2 回答 2

1

这是一个解压缩功能,可能对您的情况有所帮助,看看这是否适合您。

def create_photos_from_zip logger.debug "create_photos_from_zip -->" zip = params[:zip] logger.debug "解压文件 #{zip.path} 类 #{zip.class}"

dir = File.join(RAILS_ROOT,"public","events","temp",current_user.name)
FileUtils.mkdir_p(dir)
Zip::ZipFile.open(zip.path).each do |entry|
  # entry is a Zip::Entry
  filename = File.join(dir,entry.name)
  logger.debug "will extract file to #{filename} to"
  entry.extract(filename)
  p = File.new(filename)
  photo = Photo.create(:photo=>p)
  photo.title = "nova imagem"
  @event.photos << photo 
  p.close
  FileUtils.remove_file(filename)
end
#delete zip file
logger.debug "closing/deleting zip file #{zip.path}"
zip.close
FileUtils.remove_file(zip.path)
logger.debug "saving the event to database"
logger.debug "create_photos_from_zip <--"

结尾

于 2015-01-30T10:38:14.893 回答
0

Not sure if this would help as I am struggling with paperclip myself but here goes.

I would expect that after the save method has been called and completed that the file would be saved. I would suggest that you call your additional actions on the file after the save method.

Hope it helps.

于 2013-01-07T17:49:22.940 回答