#This is the sample appliocation code ,Please change as per your requirements.
==========For Document uploading============
1. model
-dcument.rb
def upload=(input_data)
if input_data != “”
self.filename = input_data.original_filename
self.documenttype = input_data.content_type.chomp
self.content = input_data.read
self.documentsize = input_data.size
end
end
2. controller
#display the doc /dopwnlaod
def showdoc# for download document
doc_id = params[:id]
#@doc = Document.find(doc_id)
#send_data(@doc.content, :type => @doc.documenttype,
# :filename => @doc.filename,
# :disposition => ‘inline’)
#
if doc_id != 0
begin
@doc = Document.find(doc_id)
if !(@doc.documentsize).blank?
send_data(@doc.content, :type => @doc.documenttype,
:filename => @doc.filename,
:disposition => ‘disposition’)
#increment download count
@count =(@doc.downloadcount)+1
@up = Document.update(doc_id, :downloadcount => @count)
#end incerement
return
end
rescue
end
end
flash[:notice] = “Error occured during downlaoding.”
redirect_to :action => “index”
end
#save document
def savedoc
if params[:doc][:title].blank?
flash[:notice] = “Title cannot be left blank.”
elsif params[:doc][:upload].blank?
flash[:notice] = “Please select Document.”
else
@doc = Document.new(params[:doc])
if @doc.save
flash[:notice] = “Document was successfully created.”
redirect_to :action => “index”
return
else
flash[:notice] = “Error occured during Uploading.”
end
end
redirect_to :action => “adddoc”
end
#update document
def updatedoc
if params[:doc][:title].blank?
flash[:notice] = “Title cannot be left blank.”
else
@items = Document.find(params[:id])
if @items.update_attributes(params[:doc])
flash[:notice] = ‘Document was successfully updated.’
redirect_to :action => “index”
return
end
end
redirect_to :action => “editdoc”,:id=>params[:id]
end
3. view
-showdocument.rhtml
<% @items_all = Document.find(:all)
for @items in @items_all %>
<%= link_to ‘Download’, :id => @items.id, :action => “showdoc” %>
<% end %>
-adddocument.rhtml
<%= form_tag ({:action => ’savedoc’},:multipart => true) %>
<table>
<tr><td>Title</td>
<td><%= text_field (“doc”,”title”,:size=>”30″) %><span id=”red”> *</span>
</td></tr>
<tr><td>Description</td>
<td><%= text_area (“doc”,”description”,:cols=>”28″,:rows=>4)%>
</td></tr>
<tr><td>Upload Document</td><td>
<%= file_field ‘doc’, ‘upload’, :size => “20″ %><span id=”red”> *</span>
</td></tr>
</tr>
<tr><td colspna=”2″> </td></tr>
<tr><td colspna=”2″><input type=”submit” name =”save” value=”Add”></td></tr>
</table>
</form>
4. database table structure
-document table
id=int
title=varchar
description=varchar
filename-varchar
documenttype=varrchar
documentsize-bigint(11)
content – longblob
downlaodcount-bigint(11)