如何下载使用载波上传的特定文件?
How can I download the particular file which is uploaded using carrierwave?
您好,我正在使用 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"
我加个例子,看这个就可以解决你的问题
在您看来=>
<%= 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
这样解决了
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 %>
您好,我正在使用 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"
我加个例子,看这个就可以解决你的问题
在您看来=>
<%= 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
这样解决了
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 %>