0

模型 ..

def self.upload_client_document(params)
  client_document = self.new :uploaded_data => params[:Filedata],:client_id => params[:client_id],:document_name => self.default_document_name,:document_description => self.default_description_name
  client_document.rename_document_name
  client_document.save!
  # RAILS_ROOT + client_document.public_filename This will return path to file like
  # C:/projects/test_project/client_documents/0000/0012/A100-bal.csv
  RestClient.post 'http://demo.testltd.com/V12345/search.aspx?username=test1&password=test2',
  :upload => File.new(RAILS_ROOT + client_document.public_filename)  
end

出现错误 Errno::ENOENT(没有这样的文件或目录 - C:/projects/test_project/client_documents/0000/0012/A100-bal.csv):

但是文件夹中有文件...

任何想法 ?这段代码有什么问题?我需要为 rest-client 修改什么?

4

1 回答 1

-1

File.new创建一个文件。你要File.read

RestClient.post 'http://demo.testltd.com/V12345/search.aspx?username=test1&password=test2',
  :upload => File.read(RAILS_ROOT + client_document.public_filename)  

另外:RAILS_ROOT 也被弃用了。使用 Rails.root 另外:不应该信任 client_document.public_filename 不包含某些内容,例如“../../”

... 所以 ...

:upload => File.read(Rails.root.join(File.basename(client_document.public_filename)))
于 2011-01-12T01:37:02.550 回答