gpt4 book ai didi

ruby-on-rails - Rails 5.2 使用 ActiveStorage 和 Creek 导入 XLSX

转载 作者:行者123 更新时间:2023-12-04 06:27:35 27 4
gpt4 key购买 nike

我有一个名为 ImportTemp 的模型,用于将导入的 XLSX 文件存储到数据库中。我正在使用 ActiveStorage 来存储文件。

这是模型代码:

class ImportTemp < ApplicationRecord
belongs_to :user
has_one_attached :file
has_one_attached :log_result
end

这是我的导入 Controller 代码:

def import
# Check filetype
case File.extname(params[:file].original_filename)
when ".xlsx"
# Add File to ImportFile model
import = ImportTemp.new(import_type: 'UnitsUpload', user: current_user)
import.file.attach(params[:file])
import.save

# Import unit via sidekiq with background jobs
ImportUnitWorker.perform_async(import.id)

# Notice
flash.now[:notice] = "We are processing your xlsx, we will inform you after it's done via notifications."
# Unit.import_file(xlsx)
else flash.now[:error] = t('shared.info.unknown')+": #{params[:file].original_filename}"
end
end

上传 xlsx 文件后,将在 sidekiq 中处理导入。这是工作代码(实际上仍然没有执行导入):

class ImportUnitWorker
include Sidekiq::Worker
sidekiq_options retry: false

def perform(file_id)
import_unit = ImportTemp.find(file_id)

# Open the uploaded xlsx to Creek
creek = Creek::Book.new(Rails.application.routes.url_helpers.rails_blob_path(import_unit.file, only_path: true))
sheet = creek.sheets[0]

puts "Opening Sheet #{sheet.name}"
sheet.rows.each do |row|
puts row
end

units = []

# Unit.import(units)
end

但是在我尝试之后,它给了我错误:

Zip::Error (File /rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3960b6ba5b55f7004e09967d16dfabe63f09f0a9/2018-08-10_10_39_audit_gt.xlsx not found)

但是如果我尝试用我的浏览器打开它,链接看起来是这样的:

http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3960b6ba5b55f7004e09967d16dfabe63f09f0a9/2018-08-10_10_39_audit_gt.xlsx

它正在运行并且 xlsx 已下载。我的问题是它有什么问题?为什么在 sidekiq 中找不到该文件?

最佳答案

我最终按照 George Claghorn 的建议使用了 Tempfile。我不知道这是否是最佳解决方案或最佳实践,但它现在对我有用。我将在等待 Rails 6 稳定版推出具有 ActiveStorage::Blob#open 功能的同时使用此解决方案。

def perform(file_id)
import = ImportTemp.find(file_id)
temp_unit = Tempfile.new([ 'unit_import_temp', '.xlsx' ], :encoding => 'ascii-8bit')
units = []

begin
# Write xlsx from ImportTemp to Tempfile
temp_unit.write(import.file.download)

# Open the temp xlsx to Creek
book = Creek::Book.new(temp_unit.path)
sheet = book.sheets[0]

sheet.rows.each do |row|
# Skip the header
next if row.values[0] == 'Name' || row.values[1] == 'Abbreviation'
cells = row.values

# Add cells to new Unit
unit = Unit.new(name: cells[0], abbrev: cells[1], desc: cells[2])
units << unit
end

# Import the unit
Unit.import(units)
ensure
temp_unit.close
temp_unit.unlink # deletes the temp file
end
end

关于ruby-on-rails - Rails 5.2 使用 ActiveStorage 和 Creek 导入 XLSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51815123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com