Textfile upload

I am having trouble finding things online on how to do a simple file
upload. Could you guys give me some advice? Point me in right direction.

Hi,
I posted a similar question quite recently, as all the tutorials I found
online were incorrect.

I have some basic code that works for uploading files, which I have
pasted below. It has no error checking of any kind at the moment.

Hope it helps,
Jen.

Model:

class DataFile < ActiveRecord::Base
#takes the upload object and extracts the file.
def self.save(upload)
name = upload[‘upload’].original_filename
directory = “public/data/upload”
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, “wb”) { |f| f.write(upload[‘upload’].read) }
end
end

Controller:
class UploadController < ApplicationController
def index
#Renders a fiew for the user.
render ‘upload.rhtml’
end
def create
#posts to the data model.
post = DataFile.save(params[:upload])
puts “file uploaded”
render ‘upload’
end
end

View:

File Upload

<% form_for(:upload, :url=>{ :controller=>"upload", :action=>"create"}, :html => {:multipart => true}) do |f| %> <%= f.label :"File to upload" %> <%= f.file_field :upload%> <%= f.submit %> <% end %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs