如何下载使用carrierwave上传的特定文件?

时间:2016-06-04 07:37:22

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

您好我正在使用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

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

显示我想要做的事情。选择文件上传然后下载。 enter image description here

我的代码是:

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"

2 个答案:

答案 0 :(得分: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 %>

答案 1 :(得分:0)

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

在您的视图中=&gt;

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

在你的控制器中=&gt;

     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