When uploading a file, you don't need to save it to disk on the server - simply read the upload IO (see here):
view.erb:
<%= form_tag({action: :upload}, multipart: true) do %>
<%= file_field_tag 'xml' %>
<% end %>
<%= form_for @upload do |f| %>
<%= f.file_field :xml %>
<% end %>
controller:
uploaded_io = params[:upload][:xml]
string_complete = uploaded_io.read.gsub(whatever)
For downloading, you also don't need to save to disk - simply serve the new file contents (see here)
send_data string_complete, filename: 'EditXML.xml'
If the files were uploaded to a directory in some other way, you can scan the input directory using Dir.glob
, and then iterate over each, creating a new file in the output file with the name based on the input file's name:
Dir.glob(File.join(input_directory, '*.xml')).each do |input_file|
stringComplete = File.read(input_file,encoding: "UTF-8").gsub(my mystery gsub)
input_file_basename = File.basename(input_file)
newfile = File.open(File.join(output_directory, input_file_basename + '_output.xml', "w")
newfile.puts "method(xyz)"
end