Dropzone Rails Gem如何发送0个文件

发布于 2025-02-13 06:11:28 字数 1025 浏览 2 评论 0原文

我想知道正确的方法,即使我上传了0个文件,我仍将如何处理仍在发送。我的表格具有大量的输入数据,并且图像是可选的。

我可能可以使用完全独立的AJAX请求发送,该请求的重复是“ SendingMultiple”,否则我不确定在处理此情况时,在Dropzones Solutions的范围内是否有可能。

  Dropzone.options.dropzone =
    url: '/test/create'
    autoProcessQueue: false
    clickable: true
    uploadMultiple: true
    timeout: 180000
    parallelUploads: 8
    maxFiles: 8
    maxFilesize: 5
    acceptedFiles: 'image/*'
    addRemoveLinks: true
    headers: 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
    init: ->
      dzClosure = this

      document.getElementById('submit-button').addEventListener 'click', (e) ->
        e.preventDefault()
        e.stopPropagation()
        dzClosure.processQueue()
        return

      @on 'sendingmultiple', (data, xhr, formData) ->
        formData.append 'item_number', jQuery('#item_number').val()
        ... more form data
        return
      @on 'success', (file, responseText) ->
        window.location.href = url
        return
      return

I want to know the right way on how I handle still sending even when I have 0 files uploaded. I have a form that has a lot of data to input and the images are optional.

I could probably send this with an entirely separate Ajax request that is a duplicate of 'sendingmultiple', otherwise I am unsure if this is possible within the confines of dropzones solutions in handling this case.

  Dropzone.options.dropzone =
    url: '/test/create'
    autoProcessQueue: false
    clickable: true
    uploadMultiple: true
    timeout: 180000
    parallelUploads: 8
    maxFiles: 8
    maxFilesize: 5
    acceptedFiles: 'image/*'
    addRemoveLinks: true
    headers: 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
    init: ->
      dzClosure = this

      document.getElementById('submit-button').addEventListener 'click', (e) ->
        e.preventDefault()
        e.stopPropagation()
        dzClosure.processQueue()
        return

      @on 'sendingmultiple', (data, xhr, formData) ->
        formData.append 'item_number', jQuery('#item_number').val()
        ... more form data
        return
      @on 'success', (file, responseText) ->
        window.location.href = url
        return
      return

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2025-02-20 06:11:29

好吧,我找到了一个我认为最好的解决方案。其他任何人都有更好的东西,请提供。

  document.getElementById('submit-button').addEventListener 'click', (e) ->
    uploaded_files = $('.dz-preview').length
    if uploaded_files > 0
      e.preventDefault()
      e.stopPropagation()
      dzClosure.processQueue()
      return

我只是更改了它,因此只有在检测到适合我的情况的任何图像预览时,我们只会覆盖此功能的按钮。因此,我们仍然对控制器上的正常创建/更新是正常的,当没有图像上传时会触发。然后,我在控制器中有一个image_upload_create函数,该功能根据表单中的ID是否具有ID来更新或创建。

对于那些对语法感到困惑的人,该解决方案都是在咖啡本中。

Okay I have found a solution that I think works the best. Anyone else with something better please provide.

  document.getElementById('submit-button').addEventListener 'click', (e) ->
    uploaded_files = $('.dz-preview').length
    if uploaded_files > 0
      e.preventDefault()
      e.stopPropagation()
      dzClosure.processQueue()
      return

I just changed it so we only override the button to this function if we detect any image previews, which works for my situation. So we still have the normal create/update on the controller as normal that fires when no images are uploaded. Then I have an image_upload_create function in the controller that updates or creates depending if it has a id in the forms.

The solution is all in coffeescript for those confused about syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文