1

I have to upload multiple files as form request. I am using the Rest Client to post my request. I am able to upload single file but I am not sure how to add multiple files in a single request.

I searched/googled for such option and I am not finding any solution that solves my problem.

Below is my code.

It has variable argument (*yamlfile) which takes one or more files. I have to upload all the files together.

The issue now is , I am getting syntax error when I add the loop to extract the file within the payload. my assumption is now to form this outside the payload and include it inside the payload block but I am not sure how to do it. Can someone help me with that. ( I have tried net/http/post/multipart library too and I don't find much documents around it)

def uploadRest(endpoint,archive_file_path,,yaml_file_path,*yamlfile)

  $arg_len=yamlfile.length

  request = RestClient::Request.new(
          :method => :post,
          :url => endpoint,
          :payload => {
            :multipart => true,
            :job_upload_archive => File.new(archive_file_path,'rb'),
            :job_upload_path => "/tmp",

           # Trying to add multiple file, but I get syntax error  
           yamlfile.each_with_index { |yaml, index|
              :job_upload_yaml_file+index => File.new("#{yaml_file_path}/#{pmml}")

              }

          })
  response=request.execute
  puts response.code

end  
4

4 回答 4

1
uploadRest(endpoint,archive_file_path,yaml_file_path,*yamlfile) 
@files=Array.new
yamlfile.each{ |yaml_file| 
          @files.push(File.new("#{yaml_file_path}/#{yaml_file}")) 
}
request = RestClient::Request.new(
      :method => :post, 
      :url => endpoint, 
      :payload => { :multipart => true, :job_upload_archive => File.new(archive_file_path,'rb'), 
:job_upload_path => "/tmp", :job_upload_yaml_file => @files }) 

    response=request.execute 
end
于 2013-06-04T23:47:45.217 回答
1

我遇到了类似的问题,并且能够通过将数组数组作为请求传递来使其工作。

 file1 = File.new("#{yaml_file_path}/#{yaml_file1}", 'rb')
 file2 = File.new("#{yaml_file_path}/#{yaml_file}", 'rb')

request_body = [["files", file1], ["files", file2]]
RestClient.post url, request_body, request_headers
于 2016-12-06T21:43:40.903 回答
0

您的问题代码有两个问题:

1) 尝试将符号添加到整数

2)尝试将 yamlfile 的内容直接插入到哈希中(因为这就是yamlfile.each_with_index 返回的内容,而不是它如何调用您的块。不使用块的返回值)

这两个代码问题看起来好像您已经获得了 HAML 或其他模板语言的经验,并且正在使用可以在其中工作的结构/想法?

Ruby 中有许多可能的解决方案,但是一种简单的方法来构建部分散列,而不是通过嵌入巧妙的散列返回例程一次性生成它。尝试这样的事情:

payload_hash = {
  :multipart => true,
  :job_upload_archive => File.new(archive_file_path,'rb'),
  :job_upload_path => "/tmp",
}

# This does not use the return value from each_with_index, instead it relies
# on the block to make changes to the hash by adding new key/value pairs
yamlfile.each_with_index { |yaml, index|
  # This concatenates two strings, and then converts the combined
  # string into the symbol that you want
  file_key = ("job_upload_yaml_file"+index.to_s).to_sym
  payload_hash[file_key] = File.new("#{yaml_file_path}/#{yaml}")
}

request = RestClient::Request.new(
      :method => :post,
      :url => endpoint,
      :payload => payload_hash
)

为了增加代码的清洁度,您可以将前两部分作为一个单独的方法,并在它当前拥有的地方调用它payload_hash

这应该可以帮助您克服当前的语法障碍。但是,我没有尝试检查这是否允许您通过 RESTClient 上传多个文件。

于 2013-06-04T20:37:48.080 回答
-1

第 1 节:

@params = { "FacialImage" => UploadIO.new(File.new('C:\temp\ATDD\Test\test\sachin.jpg'), "image/jpeg"), "Login" => UploadIO.new (File.new('C:\temp\ATDD\Test\test\login.txt'), "application/json") }

于 2015-08-26T15:49:17.770 回答