诗歌rails之文件上传
1,upload_controller.rb
2,upload.rhtml
- 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
- <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 %>
莫愁前路无知己,天下无人不识君。