這裏是文件創建和編輯窗體的一部分。忽略顯而易見的自定義功能。
<%=form_for fs_object,:url=>(fs_object.id ? fs_object_path(fs_object) : fs_objects_path),:html=>{:multipart=>true} do |f|%>
<%=p.prop_capture :image do %>
<%FsFile.images.each do |i|%>
<%= f.radio_button(:image, i)%><%=image_tag i,:height=>50%>
<%end%>
<%end%>
<%=p.prop "file upload",f.file_field(:content_binary)%>
<%=link_to "download",download_fs_object_url(@object,:action=>"download"),:remote=>@js,:method=>:post if @object.id%>
<%=p.prop :content_type %>
<%end%>
這是控制器創建操作來創建文件或文件夾。 該文件的二進制內容由handle_content管理。
def create
p=params[:fs_folder]||params[:fs_file]
@object =if params[:fs_folder]
FsFolder.new(p)
elsif params[:fs_file]
FsFile.handle_content p
h=FsFile.new(p)
end
end
句柄內容,管理二進制內容和FSFile中的內容類型。
def handle_content p
if(p)
if p[:content_binary]
p=self.class.handle_content(p)
if !p[:content_ending]
p[:content_ending]=content_ending
end
end
end
p
end
def self.handle_content p
if(p)
if p[:content_binary]
logger.info "UPLOADED: #{p[:content_binary]}"
c=p[:content_binary].read
ext=File.extname p[:content_binary].original_filename
p[:content_ending]=ext
type=File.mime_type? p[:content_binary].original_filename
debug "'#{p[:content_binary].original_filename}' '#{p[:name]}'"
p[:name]=p[:content_binary].original_filename if !p[:name]||p[:name].strip==""
# type=ext.match(/od./) ? "Open Document file" : type
p[:content_type]=type
# p[:image]=get_image_for_type p[:content_type] if !p[:image]
p[:content_binary]=c
end
end
p
end
在我的Gemfile我添加的MIME類型:
gem 'mimetype-fu'
我希望幫助。
呃,對。我已將它更改爲form_for(@admin_article,:html => {:multipart => true}),併爲文檔列添加了一個自定義訪問器,用於將文件名和數據拆分爲字段。 – cytrinox 2011-03-07 10:52:19