I need to take a .csv file from an HTML form and parse through all the data.
Right now I am using the CSV gem in a ruby script to parse a local file and post it to a Pardot form handler with this code:
require 'net/http'
require 'uri'
require 'csv'
uri = URI('<form handler url>')
CSV.foreach("<csv file location>", :headers => :first_row) do |row|
res = Net::HTTP.post_form(uri, 'city' => row['city'],
'state' => row['state'],
'zip' => row['zip'],
'country' => row['country'],
'phone' => row['phone'],
'fax' => row['fax'],
'website' => row['website'],
'first_name' => row['first_name'],
'last_name' => row['last_name'],
'job_title' => row['job_title'],
'Company' => row['Company'],
'leadtype' => row['leadtype'],
'tradeshow_attended' => row['tradeshow_attended'],
'industry' => row['industry'],
'email' => row['email'],
'boothsize' => row['boothsize'],
'company' => row['company'],
'source' => row['source'],
'address_one' => row['address_one'],
'address_two' => row['address_two']
)
end
Now I have created a Sinatra site with a simple form that has a text input for url and a file upload for the csv file.
What I want to do is pull the CSV file’s data into the script above.
Here is my form:
<form action="" enctype="multipart/form-data" method="post">
<p>
<label>Form Handler URL</label><input name="url" type="text"/>
</p>
<p>
<label>Please specify a file</label>
<input type="file" name="datafile">
</p>
<div>
<input type="submit" value="Submit">
</div>
</form>
I can get the file information using
params[:datafile]
How do I get the text of a file uploaded through this form? The file is not going to be saved on the server.
it so happens the file is already saved on the server (albeit to a temporary file as a tempfile object). you can access it through
:tempfilein the params-hash: