Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the contents of temp file through a form

index.html.erb

= form_for :file_upload, :html => {:multipart => true} do |f|
      = f.label :uploaded_file, 'Upload your file.'
      = f.file_field :uploaded_file
      = f.submit "Load new dictionary"

Model

def file_upload
    file = Tempfile.new(params[:uploaded_file])
    begin
        @contents = file
    ensure
        file.close
        file.unlink   # deletes the temp file
    end
end

Index

def index
    @contents
end

But nothing is getting printed in my page after I upload a file = @contents

like image 924
ahmet Avatar asked May 20 '13 11:05

ahmet


1 Answers

Use file.read to read the content of the uploaded file:

def file_upload
  @contents = params[:uploaded_file].read
  # save content somewhere
end
like image 143
aromero Avatar answered Oct 28 '22 03:10

aromero