【问题标题】:How can I download the particular file which is uploaded using carrierwave?如何下载使用carrierwave上传的特定文件?
【发布时间】:2016-06-07 19:32:50
【问题描述】:

您好,我使用的是 Ruby 2 和 Rails 4。我正在上传一个 pdf 文件并将其存储在 public/uploads/contact/file/15/abc.pdf,因为我使用的是 CarrierWave,并且在我的 file_uploader.rb 中编写了以下代码。

def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end

现在我想下载我上传的同一个文件。我怎样才能下载那个文件?如果有人有任何想法,请与我分享。我在下面添加我的代码。

显示图片我想要做什么。选择一个文件上传它然后下载它。

我的代码是:

downloads_form.html.erb

<table class="table table-condensed table-responsive">
        <tbody>
            <%= form_tag create_form_path,  multipart: true  do |f| %>
            <tr>                
                <td>ABC</td>
                <td><%= file_field_tag "contact[file]",  class: "form-control" %></td>
                <td><%= submit_tag 'Upload' %></td>
                <td><%= link_to "Download", static_pages_downloadform_pdf_path %></td>

            </tr>
            <tr>               
                <td>DEF</td>
                <td><%= file_field_tag "contact[file]",  class: "form-control" %></td>
                <td><%= submit_tag 'Upload' %></td>
                <td><%= link_to "Download", static_pages_downloadform_pdf_path %></td>
            </tr>
            <% end %> 

        </tbody>
    </table>

static_pages_controller.rb

  def create_form
    @form_downup = Contact.new(contact_params)
    if @form_downup.save
      redirect_to :back
    else
      render downloads_form_path
    end
  end

  def downloadform_pdf

  end

 private
    def contact_params
       params.require(:contact).permit(:file)

    end

routes.rb

  match '/downloads_form', to: 'static_pages#downloads_form', via: 'get', as: :downloads_form
  match '/create_form', to: 'static_pages#create_form', via: 'post', as: :create_form     
  get "static_pages/downloadform_pdf"

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave ruby-on-rails-4.2


    【解决方案1】:

    这样就解决了。

    def downloadform_pdf
    
        file_type=params[:file_name]
    
        contact = Contact.find_by_name(file_type)
        actual_file_name=contact.file.to_s
        file = File.join(Rails.root, 'public',actual_file_name)
        send_file(file, filename: 'My-Form.pdf', type: 'application/pdf')
    
      end
    

    查看:

    <%= form_tag create_form_path,  multipart: true  do |f| %> 
       <td><%= text_field_tag "contact[name]", nil, value: "G_form", class: "form-control", :readonly => true %></td>
       <td><%= file_field_tag "contact[file]",  class: "form-control" %></td>
       <td><%= submit_tag 'Upload' %></td>
       <td><%= link_to "Download", static_pages_downloadform_pdf_path(:file_name => "G_form") %></td> 
    <% end %>
    

    【讨论】:

    • 对此我无法表达我的感激之情。我花了几天时间试图弄清楚这一点!谢谢!
    【解决方案2】:

    我正在添加一个示例,通过查看您可以解决您的问题

    在你看来 =>

    <%= link_to "click here to download", signed_feeds_pdf_path(:feed_image_path => feed_image.feedimage.path), target: '_self' %>
    

    在您的控制器中 =>

         def pdf    
            file_name = params[:feed_image_path].split('/').last
            @filename ="#{Rails.root}/public/uploads/feed_image/feedimage/#{file_name}"
            send_file(@filename ,
              :type => 'application/pdf/docx/html/htm/doc',
              :disposition => 'attachment')           
          end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 2011-11-10
      • 2013-06-30
      • 2012-12-20
      • 2015-09-19
      • 2018-07-08
      • 2011-10-06
      相关资源
      最近更新 更多