Parsing a textfile in Ruby on Rails w/o saving it
When you upload a file, Rails will create a file in the tmp
directory, which will later be deleted automatically. Therefore, you don't have to do anything for the upload to be automatically deleted. That means that if you wanted to keep the file, you would need to copy it manually to a permanent location.
You can access the temporary file by calling the tempfile
method on params[:file_upload]
, for example:
class UploadController < ApplicationController
def new
# upload form
end
def create
data = params[:file_upload].tempfile.read
parts = data.split(">>>>\n")
# do something with parts
end
end
Rails - Reading uploaded text file
You should add multipart to your form just like the following
<%= form_tag '/greetings/hello', multipart: true do %>
Import CSV files into SQL database in ROR
Your rake task looks more like a regular ruby script than a rake task. If you want to use rake for this, try something like:
require 'csv'
namespace :import do
desc 'Imports csv data into db'
task from_csv: :environment do
csv_text = File.read('/Path/to/csv/Item.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
Item.create!(row.to_hash)
end
end
end
and invoke it with rake import:from_csv
Uploading a txt file without saving it to database using ajax
If you want to upload file with AJAX and rails forms you can use remotipart
gem:
Remotipart is a Ruby on Rails gem enabling AJAX file uploads with
jQuery in Rails 3.0 and Rails 3.1 remote forms. This gem augments the
native Rails jQuery remote form functionality enabling asynchronous
file uploads with little to no modification to your application.
For me it works with Rails 3.2 as well.
https://github.com/JangoSteve/remotipart
http://rubygems.org/gems/remotipart
how to read a User uploaded file, without saving it to the database
You are very close. Check the class type of params[:uploaded_file]
, it should typically be either a StringIO
or a Tempfile
object -- both of which already act as files, and can be read using their respective read
method(s).
Just to be sure (the class type of params[:uploaded_file]
may vary depending on whether you are using Mongrel, Passenger, Webrick etc.) you can do a slightly more exhaustive attempt:
# Note: use form validation to ensure that
# params[:uploaded_file] is not null
file_data = params[:uploaded_file]
if file_data.respond_to?(:read)
xml_contents = file_data.read
elsif file_data.respond_to?(:path)
xml_contents = File.read(file_data.path)
else
logger.error "Bad file_data: #{file_data.class.name}: #{file_data.inspect}"
end
If, in your case, it turns out that params[:uploaded_file]
is a hash, make sure that you have not mistakingly flipped the object_name
and method
parameters when invoking file_field
in your view, or that your server is not giving you a hash with keys like :content_type
etc. (in which case please comment on this post with the Bad file_data ...
output from development.log
/production.log
.)
Related Topics
Simplest Way to Send Raw Byte-Arrays Using Ruby's Tcpsocket-Class
Can't Convert String into Integer in Ruby/Ruby-On-Rails
MAC Osx Lion and Ruby - [Fatal] Failed to Allocate Memory
How to Decode a Cookie from the Header of a Websocket Connection Handshake? (Ruby)
What Are the Ruby Win32API Parameters | How to Pass a Null Pointer
Trying to Get Content Inside Cdata Tags in Xml File Using Nokogiri
Failure: Expected 0 to Be >= 1 on Ruby on Rails
Does Ruby 1.9.2 Have an Is_A? Function
Is Systemexit a Special Kind of Exception
How to Upload a File with Watir and Ie
Encryption-Decryption in Rails
How to Rescue from a 'Require': No Such File to Load in Ruby
How to Find Current Abstract Route in Rails Middware
How to Setup a Local Ssl Certificate and a Rails Application