简体   繁体   English

从载波获取文件和文件名

[英]Get file and filename from carrierwave

I successfully uploaded a file to S3 and now I want to attach file to email, so i can send it by using Gmail API. 我已成功将文件上传到S3,现在我想将文件附加到电子邮件,因此可以使用Gmail API发送文件。 Some people suggested to call method .file directly like here 有些人建议调用方法.file一样,直接在这里

as you can see, you can call method 如您所见,您可以调用方法

<%= @page.form.file.filename %>

.file directly from form. .file直接来自表单。

here is my mount uploader class: 这是我的mount uploader类:

class Crm::LogEmailAttachment < Asset
    belongs_to :owner, class_name: 'Crm::LogEmail'
    mount_uploader :data, Crm::LogEmailAttachmentUploader
    mount_base64_uploader :data, Crm::LogEmailAttachmentUploader
    # default_scope -> {order("assets.order")}

    def to_s
      self.data.try(:file).try(:filename)
    end
end

and here is my erb form: 这是我的erb表格:

<div class="document-wrap">
   <%= log_email.fields_for :crm_log_email_attachments do |a| %>
    <%= a. file_field :data, input_html: {class: "form-control" , wrapper: 'field'}, label: false %>
   <% end %>
</div>

then I try to get file by just calling .file from params[:data] it returns undefined method file for #<ActionDispatch::Http::UploadedFile:0x007fc337322290> , my question is how can I get the path of the file and filename from carrierwave ? 然后我尝试通过从params[:data]调用.file来获取文件,它为#<ActionDispatch::Http::UploadedFile:0x007fc337322290>返回未定义的方法file ,我的问题是如何获取文件和文件名的路径来自carrierwave

Instead of referencing the params ActionDispatch::Http::UploadedFile object, you should save the data to S3 via carrierwave first, then reference the uploader object directly. 您应该先通过载波将数据保存到S3,然后再直接引用上载器对象,而不是引用参数ActionDispatch :: Http :: UploadedFile对象。 For instance 例如

object.data.file.filename

To retrieve the filename, and 要检索文件名,并

object.data.url

To retrieve the S3 URL for the associated asset. 检索关联资产的S3 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM