本文介绍了将活动的存储附件下载到光盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指南 说我可以将附件保存到光盘以运行像这样处理它:

The guide says that I can save an attachment to disc to run a process on it like this:

message.video.open do |file|
  system '/path/to/virus/scanner', file.path
  # ...
end

我的模型有一个附件定义为:

My model has an attachment defined as:

has_one_attached :zip

然后在我定义的模型中:

And then in the model I have defined:

def process_zip      
  zip.open do |file|
    # process the zip file
  end
end

但是我收到一个错误:

private method `open' called

在 zip.open 调用中.

on the zip.open call.

如何将 zip 保存在本地进行处理?

How can I save the zip locally for processing?

推荐答案

作为 Rails 5.2 中的替代方案,您可以这样做:

As an alternative in Rails 5.2 you can do this:

def process_zip      
   # Download the zip file in temp dir
   zip_path = "#{Dir.tmpdir}/#{zip.filename}"
   File.open(zip_path, 'wb') do |file|
       file.write(zip.download)
   end   

   Zip::File.open(zip_path) do |zip_file|  
       # process the zip file
       # ...
       puts "processing file #{zip_file}"
   end
end

这篇关于将活动的存储附件下载到光盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:10