简体   繁体   English

Active Storage Direct Upload 正在使用简单表单的上传期间删除文件输入 - Rails 6

[英]Active Storage Direct Upload is removing the file input during upload w/Simple Form - Rails 6

I managed to get direct uploads working with simple_form gem, using an input options helper method.我设法使用输入选项帮助器方法使用 simple_form gem 直接上传。 The issue I am having, is that starting a direct upload will cause the progress bar to show in place of the file input and I am unable to re-upload another file if that file input has the wrong file attached.我遇到的问题是,开始直接上传将导致进度条显示在文件输入的位置,如果该文件输入附加了错误的文件,我将无法重新上传另一个文件。 Essentially, I am trying to replace files that may have been uploaded - after the fact.本质上,我正在尝试替换可能已上传的文件 - 事后。 How can I stop direct upload from replacing the file input with the progress bar?如何停止直接上传用进度条替换文件输入?

direct_upload_file_input.rb direct_upload_file_input.rb

class DirectUploadFileInput < SimpleForm::Inputs::FileInput 
 def input_html_options
  super.merge({direct_upload: true})
 end 
end

_form.html.erb _form.html.erb

has_one :media_file

  <%= f.input :media_file, as: :file, input_html: {:onchange => "activate_progress_bar", direct_upload: true}, label: false %>

direct_upload.js直接上传.js

// direct_uploads.js // direct_uploads.js

addEventListener("direct-upload:initialize", event => {
  const { target, detail } = event
  const { id, file } = detail
  target.insertAdjacentHTML("beforebegin", `
    <div id="direct-upload-${id}" class="direct-upload direct-upload--pending">
      <div id="direct-upload-progress-${id}" class="direct-upload__progress" style="width: 0%"></div>
      <span class="direct-upload__filename"></span>
    </div>
  `)
  target.previousElementSibling.querySelector(`.direct-upload__filename`).textContent = file.name
})

addEventListener("direct-upload:start", event => {
  const { id } = event.detail
  const element = document.getElementById(`direct-upload-${id}`)
  element.classList.remove("direct-upload--pending")
})

addEventListener("direct-upload:progress", event => {
  const { id, progress } = event.detail
  const progressElement = document.getElementById(`direct-upload-progress-${id}`)
  progressElement.style.width = `${progress}%`
})

addEventListener("direct-upload:error", event => {
  event.preventDefault()
  const { id, error } = event.detail
  const element = document.getElementById(`direct-upload-${id}`)
  element.classList.add("direct-upload--error")
  element.setAttribute("title", error)
})

addEventListener("direct-upload:end", event => {
  const { id } = event.detail
  const element = document.getElementById(`direct-upload-${id}`)
  element.classList.add("direct-upload--complete")
})

In your direct-upload:initialize method you're replacing the target with the progress bar div.在您的direct-upload:initialize方法中,您将target替换为进度条 div。 The target in this case is set to the event.target which is your file input field.在这种情况下, target设置为event.target ,这是您的文件输入字段。 You need to replace or insert the progress bar on a different page element if you want to keep the file input but also display the progress bar.如果要保留文件输入但还要显示进度条,则需要在不同的页面元素上替换或插入进度条。 Something like...就像是...

let progress = document.getElementById('progress');
progress.innerHTML =
  `<div id="direct-upload-${id}" class="direct-upload direct-upload--pending">
    <div id="direct-upload-progress-${id}" class="direct-upload__progress" style="width: 0%"></div>
    <span class="direct-upload__filename"></span>
  </div>`

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

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