繁体   English   中英

使用rest-client上传文件

[英]File upload with 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修改什么?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM