1,upload_controller.rb
[code]
class UploadController < ApplicationController
before_filter :configure_charsets
def configure_charsets
@headers["Content-Type"]="text/html;charset=utf-8"
end
def upload
end
def uploadFile(file)
if !file.original_filename.empty?
@filename=getFileName(file.original_filename)
File.open("#{RAILS_ROOT}/public/upload/#{@filename}", "wb") do |f|
f.write(file.read)
end
return @filename
end
end
def getFileName(filename)
if !filename.nil?
Time.now.strftime("%Y_%m_%d_%H_%M_%S") + '_' + filename
end
end
def save
unless request.get?
if filename=uploadFile(params[:file]['file'])
render :text=>filename
end
end
end
end
[/code]
2,upload.rhtml
[code]
<h1>Upload File</h1>
<%= form_tag({:action => 'save'}, :multipart => true) %>
Upload your file: <%= file_field("file", "file") %>
<br/>
<%= submit_tag("Upload file") %>
<%= end_form_tag %>
[/code]
[code]
class UploadController < ApplicationController
before_filter :configure_charsets
def configure_charsets
@headers["Content-Type"]="text/html;charset=utf-8"
end
def upload
end
def uploadFile(file)
if !file.original_filename.empty?
@filename=getFileName(file.original_filename)
File.open("#{RAILS_ROOT}/public/upload/#{@filename}", "wb") do |f|
f.write(file.read)
end
return @filename
end
end
def getFileName(filename)
if !filename.nil?
Time.now.strftime("%Y_%m_%d_%H_%M_%S") + '_' + filename
end
end
def save
unless request.get?
if filename=uploadFile(params[:file]['file'])
render :text=>filename
end
end
end
end
[/code]
2,upload.rhtml
[code]
<h1>Upload File</h1>
<%= form_tag({:action => 'save'}, :multipart => true) %>
Upload your file: <%= file_field("file", "file") %>
<br/>
<%= submit_tag("Upload file") %>
<%= end_form_tag %>
[/code]