使用rest-client上传文件

发布于 2024-09-26 01:13:15 字数 846 浏览 8 评论 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):

但是文件在文件夹中...

知道吗?这段代码有什么问题? 我需要为休息客户端修改什么吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-10-03 01:13:15

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)))

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)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文