问题
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