【问题标题】:How to save an uploaded file to the database instead of the name如何将上传的文件而不是名称保存到数据库
【发布时间】:2011-03-07 10:01:13
【问题描述】:

我正在使用 Rails3 并创建了一个简单的文档表,其中包含 document_name(字符串)和文档(blob)列以及其他一些用于元数据的列。我已经添加了

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

到 _form.html.erb 部分。模型不变(通过rails g支架生成...)

更新和新建操作适用于所有列,但上传的文档未保存到文档列中。而是将上传文档的名称保存到文档列(而不是文件内容)。

如何将上传的二进制数据写入document字段,将上传的文件名写入document_name列?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我的猜测是你忘记在表单标签中设置:multipart=&gt;true

    您能发布整个“新”表单吗?

    【讨论】:

    • 嗯,对。我已将其更改为 form_for(@admin_article, :html => {:multipart=>true}) 并为文档列添加了一个自定义访问器,它将文件名和数据拆分到字段中。
    【解决方案2】:

    这称为上传。您可以从头开始(Rails 为上传提供了很好的支持)或使用以下 Rails 插件之一:

    【讨论】:

      【解决方案3】:

      这里是用于文件创建和编辑的表单部分。忽略明显的自定义函数。

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

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 2012-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-28
        • 2019-04-30
        • 2015-08-27
        • 2017-02-05
        相关资源
        最近更新 更多