从数据库 blob 字段中提取 PDF 并保存在 public/assets 内
Pull PDF out of database blob field and save within public/assets
我在 table 中有一个 blob
列,我知道它代表 pdf 文档。我正在尝试编写一个迁移,从 blob
字段中删除 pdf 文档,并将实际的 pdf 文档保存在 public/assets 中。我正在使用 paperclip 作为附件。
这是我收到的错误:
StandardError: An error has occurred, all later migrations canceled: "\xC4" from ASCII-8BIT to UTF-8
这是我的脚本:
class AddSomeAttachments < ActiveRecord::Migration
def up
SomeModel.all.each do |something|
if something.data.present?
FileUtils.mkdir_p(Rails.root.join('public', 'assets', 'some_models', 'attachment1', "#{something.id}" ))
end
end
SomeModel.all.each do |something|
if something.data.present?
File.open(Rails.root.join('public', 'assets', 'some_models', 'attachment1', "#{something.id}", "#{something.attachment1_file_name}"), "w+") do |file|
file << something.data
end
end
end
end
def down
raise "do not migrate down"
end
end
我确实查看了 this stack overflow question,其中询问了相同的错误消息。我确实尝试在文件顶部添加 encoding: UTF-8
,但没有做任何事情。
尝试写入二进制文件:
File.open(filename, 'wb') do |file|
我在 table 中有一个 blob
列,我知道它代表 pdf 文档。我正在尝试编写一个迁移,从 blob
字段中删除 pdf 文档,并将实际的 pdf 文档保存在 public/assets 中。我正在使用 paperclip 作为附件。
这是我收到的错误:
StandardError: An error has occurred, all later migrations canceled: "\xC4" from ASCII-8BIT to UTF-8
这是我的脚本:
class AddSomeAttachments < ActiveRecord::Migration
def up
SomeModel.all.each do |something|
if something.data.present?
FileUtils.mkdir_p(Rails.root.join('public', 'assets', 'some_models', 'attachment1', "#{something.id}" ))
end
end
SomeModel.all.each do |something|
if something.data.present?
File.open(Rails.root.join('public', 'assets', 'some_models', 'attachment1', "#{something.id}", "#{something.attachment1_file_name}"), "w+") do |file|
file << something.data
end
end
end
end
def down
raise "do not migrate down"
end
end
我确实查看了 this stack overflow question,其中询问了相同的错误消息。我确实尝试在文件顶部添加 encoding: UTF-8
,但没有做任何事情。
尝试写入二进制文件:
File.open(filename, 'wb') do |file|