使用rest-client上传文件

时间:2010-10-05 15:42:26

标签: ruby-on-rails ruby rest-client

模特..

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修改什么吗?

1 个答案:

答案 0 :(得分:-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)))