gpt4 book ai didi

ruby-on-rails - 如何对服务器上已有的文件使用回形针?

转载 作者:数据小太阳 更新时间:2023-10-29 08:34:57 25 4
gpt4 key购买 nike

郑重声明,我正在处理遗留代码,并试图在不破坏所有内容的情况下插入新功能。现在我的服务器上有一堆文件:

 myapp/public/temp/myfile.doc

问题是我想在 Controller 操作中从这些文件创建一个 Docfile 对象。

这是精简后的 Docfile 类:

class DocFile < ActiveRecord::Base
has_attached_file :docs,
:path => "#{Constants::DOCFILES_PATH}:basename.:extension",
:url => "http://#{Constants::SITE_URL}/docs/:basename.:extension"
end

如果您从表单上传,Paperclip 有一些很好的文档,但我的情况不是这样。

那么我怎样才能“模拟”我正在上传文件的事实呢?

到目前为止我已经试过了:

temp_file_url = "correct_rails_root/myapp/public/temp/myfile.doc"
@docfile = DocFile.new :docs => temp_file

但它不起作用。

如有任何指点,我们将不胜感激!

编辑:

我这样做了:

temp_file_url = Constants::TEMPORARY_UPLOAD_PATH + "/" + params[:temp_file_upload][:doc]
temp_file = File.new(temp_file_url, "w+")
@docfile = DocFile.new :docs => File.open(temp_file_url)

还是不行

最佳答案

我必须这样做:

temp_file_name = #the filename
temp_file_path = Constants::TEMPORARY_UPLOAD_PATH + "/" + temp_file_name
temp_file = File.new(temp_file_path, "r")

@docfile = DocFile.new :docs => temp_file

显然,如果我没有以“已读”方式打开文件,它就无法工作。这对我来说意义不大,但现在可以用了!

关于ruby-on-rails - 如何对服务器上已有的文件使用回形针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1804024/

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