2

I'm using the following code to upload a single file through a form:

app.rb

@filename = params[:file][:filename]
file = params[:file][:tempfile]

File.open("./public/#{@filename}", 'wb') do |f|
 f.write(file.read)
end

string_file.erb:

<input type='file' class='form-control' id='<%= array['id'] %>'name='file[]' value='<%= default_value %>' <%= constraints %> style="display: none;" multiple>

How do I loop through to write multiple files to the filesystem?

Mary P.
  • 35
  • 5
  • 1
    I would consider the size of the uploads, server limitations and application responsiveness... for example, multiple small uploads can probably use `XMLHttpRequest ` and it's `progress` event to indicate upload state, while you might consider a web socket solution to handle very large uploads, allowing uploads to resume after connection interruptions (without restarting the upload). Also, your server has a limited amount of memory and CPU... some Ruby servers might cause DoS or resource starvation when large files are uploaded while others (i.e. `iodine`) offer preemptive upload size limits. – Myst Apr 28 '17 at 02:58
  • @Myst Thanks for the suggestion, I'm adding code on client side and server side to restrict files to 10 MB or less, the files being uploaded need to be emailed as well. – Mary P. May 01 '17 at 22:11
  • Sure, you're welcome. Good luck :-) ... as a last note, I would consider that by the time the HTTP parser is done and Rack already holds the request data, the question of checking the upload file limits might be a bit too late. On the server side I would consider implementing the limit using the proxy layer (i.e. `nginx`/`apache` settings), or by using server settings where supported - [see here](http://stackoverflow.com/questions/2200188/rails-file-upload-size-limit). – Myst May 01 '17 at 23:20
  • @Myst Appreciate all of your suggestions! I'm working on implementing as many client side restrictions as I can to keep users from uploading files over 10MB as well as restricting file type. No matter the file, as soon as it's emailed I'm removing the file from the server as well. I'm using nginx and I'm setting the client_max_body_size in my conf files. – Mary P. May 03 '17 at 14:10

1 Answers1

2

this is my solution

puts params['images'].map{ |f| f[:filename] }.join(";")
k = params['images'].map{ |f| f[:filename] }.join(";")
$param = k.chomp.split(";")
array_length = $param.length       # or $param.size
array_lengthx = array_length - 1
puts "length of $param is : #{array_length}"

i = 0
i = i - 1
puts array_lengthx
puts i
while i.to_i < array_lengthx do
  i =i+1
  puts i
  @filename = params[:images][i][:filename]
  file = params[:images][i][:tempfile]
  path = "/home/user/Descargas/sinatra_ajax-master/#{@filename}"
  File.open("/home/user/Descargas/sinatra_ajax-master/#{@filename}", 'wb') do |f|
    f.write file.read
  end
end

this is html code:

<form action="/upload2" method="post" enctype="multipart/form-data">
  <input type="file" name="images[]" multiple />
  <input type="submit" />
</form>
Nakilon
  • 34,866
  • 14
  • 107
  • 142
zzero
  • 154
  • 1
  • 7