这里是用于文件创建和编辑的表单部分。忽略明显的自定义函数。
<%=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'
希望对你有所帮助。