File upload with rest-client

怎甘沉沦 提交于 2019-12-12 02:07:40

问题


Model ..

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

Getting error Errno::ENOENT (No such file or directory - C:/projects/test_project/client_documents/0000/0012/A100-bal.csv):

But file is there in the folder ...

Any idea ? What's problem with this code ? Anything i need to modify for rest-client ?


回答1:


File.new creates a file. You want File.read

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

Also: RAILS_ROOT is also deprecated. Use Rails.root Also: client_document.public_filename should not be trusted to not include something e.g. "../../"

... so ...

:upload => File.read(Rails.root.join(File.basename(client_document.public_filename)))


来源:https://stackoverflow.com/questions/3865313/file-upload-with-rest-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!