开发者

How to save an uploaded file to the database instead of the name

I'm using Rails3 and have created a simple document table with a document_name (string) and document (blob) column and a few other columns for metadata. I've added

<%= f.label :document %><br />
<%= f.file_field :document %>

to the _form.html.erb partial. The model is unchanged (generated via rails g scaffold...)

Update and New actions works fine for all columns but the uploaded document is not saved into the document column. Instead, the name of the uploaded document is saved to the document column (not the file content).

How can I write the uploaded binary data to t开发者_StackOverflow中文版he document field and the uploaded file name to the document_name column?


My guess is that you forgot to set :multipart=>true in the form tag.

Could you post the whole "new" form?


This is called upload. You can do it from scratch (Rails offers a pretty good support for uploading) or use one of the following Rails plugins:

  • Paperclip
  • CarrierWave


Here the part of the form for file creation and editing. Ignore the obvious custom functions.

<%=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%> 

This is the controller create action to create a file or a folder. The binary content of the file is managed by 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

The handle content, what manages the binary content and the content type in the 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

In my gemfile i added for the MIME types:

gem 'mimetype-fu'

I hope that helped.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜