我使用rails 3和回形针插件来附加文件。
我有两种型号:
- 秩序
- 订单附件:
- belongs_to:订单
- has_attached_file:doc
我尝试通过回形针将文件附加到订单中;
当我使用英文或数字文件名附加文件时,所有都非常有效:
SQL (0.0ms) INSERT INTO "order_attachments" ("order_id", "created_at", "updated_at", "doc_file_name", "doc_content_type", "doc_file_size", "doc_updated_at") VALUES (1, '2011-06-08 11:07:22.108523', '2011-06-08 11:07:22.108523', 'Example.txt', 'text/plain', 22, '2011-06-08 11:07:22.105523') RETURNING "id"
[paperclip] Saving attachments.
[paperclip] saving D:/my_project_path/public/system/docs/93/original/Example.txt
SQL (1.0ms) COMMIT
Completed 200 OK in 229ms (Views: 21.0ms | ActiveRecord: 7.0ms)
但是,当我附加带有俄语文件名的文件时,会发生错误:
SQL (1.0ms) INSERT INTO "order_attachments" ("order_id", "created_at", "updated_at", "doc_file_name", "doc_content_type", "doc_file_size", "doc_updated_at") VALUES (1, '2011-06-08 11:26:43.040925', '2011-06-08 11:26:43.040925', 'Пример.txt', 'text/plain', 26, '2011-06-08 11:26:43.035924') RETURNING "id"
[paperclip] Saving attachments.
[paperclip] saving D:/my_project_path/public/system/docs/94/original/Пример.txt
SQL (0.0ms) ROLLBACK
Completed in 161ms
Errno::ENOENT (No such file or directory - D:/my_project_path/public/system/docs/94/original/╨а╤Я╨б╨В╨а╤С╨а╤Ш╨а┬╡╨б╨В.txt):
app/controllers/orders_controller.rb:138:in `attachment'
回形针将文件保存到D:/my_project_path/public/system/docs/94/original/Пример.txt(我可以通过资源管理器打开它),但数据库中没有创建任何记录。也许是编码出了问题。
我使用:
- Windows 7专业x64
- PostgreSQL 9.0(UTF-8数据库编码)
- Ruby 1.9.2
- Ruby on Rails 3.0.7
- 回形针2.3.11
谢谢你的帮助。
正如我所说,回形针保存了附加的文件,rails创建了有效的请求,但回形针源中的某些内容在Windows下消失了。
Errno::ENOENT异常在第42行的C:\Ruby192\lib\rubb\gems\1.9.1\gems\paperclip-2.3.11\lib\paperclip\storage\filesystem.rb源文件中捕获
def flush_writes #:nodoc:
@queued_for_write.each do |style_name, file|
file.close
FileUtils.mkdir_p(File.dirname(path(style_name)))
log("saving #{path(style_name)}")
FileUtils.mv(file.path, path(style_name))
FileUtils.chmod(0644, path(style_name))
end
@queued_for_write = {}
end
我将此功能更改为:
def flush_writes #:nodoc:
@queued_for_write.each do |style_name, file|
file.close
FileUtils.mkdir_p(File.dirname(path(style_name)))
log("saving #{path(style_name)}")
FileUtils.mv(file.path, path(style_name))
begin
FileUtils.chmod(0644, path(style_name))
rescue Errno::ENOENT
log("Errno::ENOENT caught on #{ENV['OS']}")
end
end
@queued_for_write = {}
end
这个解决方案对我有效。现在服务器日志输出为:
SQL (1.0ms) INSERT INTO "order_attachments" ("order_id", "created_at", "updated_at", "doc_file_name", "doc_content_type", "doc_file_size", "doc_updated_at") VALUES (14, '2011-06-08 18:44:25.853559', '2011-06-08 18:44:25.853559', 'Пример.doc', 'application/msword', 292352, '2011-06-08 18:44:25.727552') RETURNING "id"
[paperclip] Saving attachments.
[paperclip] saving D:/my_project_path/public/system/docs/199/original/Пример.doc
[paperclip] Errno::ENOENT caught on Windows_NT --> !!! here it is - our exception !!!
SQL (3.0ms) COMMIT
Completed 200 OK in 287ms (Views: 13.0ms | ActiveRecord: 15.0ms)