在 ActiveStorage 中保存之前获取文件名 - Rails 5.2.1 [英] Getting filename before saving in ActiveStorage - Rails 5.2.1

查看:39
本文介绍了在 ActiveStorage 中保存之前获取文件名 - Rails 5.2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 ActiveStorage 继续保存之前获取用户上传的文件的名称.该表单是使用 form_with 生成的,如下所示:

<%= form_with 模型:上传做 |form|%><div class=""><%= form.file_field :files, multiple: true, direct_upload: true, required: true %><%= form.label :files, '', class: 'icon ion-ios-cloud-upload' do %><span>点击图标选择文件</span><%结束%><div class="actions"><%= form.submit "Upload", class: "btn btn-primary" %>

<%结束%>

我尝试访问 params[:upload][files] 并按照 此处 但我收到错误 NoMethodError: undefined method `original_filename' for #<String:0x007fac77fd18c8>.

当我检查参数时,文件确实作为字符串返回,那么我如何获取文件名或如何让 original_filename 工作?

解决方案

在附加文件后,我终于能够通过执行 file.blob.filename 获得文件名.

I am trying to get the name of the file uploaded by a user before ActiveStorage goes on to save it. The form is generated using form_with and is shown below:

<%= form_with model: upload do |form| %>
  <div class="">
    <%= form.file_field :files, multiple: true, direct_upload: true, required: true %>
    <%= form.label :files, '', class: 'icon ion-ios-cloud-upload' do %>
      <span>click the icon to select files</span>
    <% end %>

    <div class="actions">
      <%= form.submit "Upload", class: "btn btn-primary" %>
    </div>
 </div>
<% end %>

I have tried accessing params[:upload][files] and calling .original_filename on it as described here but I get the error NoMethodError: undefined method `original_filename' for #<String:0x007fac77fd18c8>.

The file does come back as a string when I inspect the params, so how do I get the filename or how do I get original_filename to work?

解决方案

I was finally able to get the file name by doing file.blob.filename after the file had been attached.

这篇关于在 ActiveStorage 中保存之前获取文件名 - Rails 5.2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆