如何让 Dropzone.js 仅在单击提交按钮时上传文件


How to get Dropzone.js to upload files only when a submit button is clicked?

在过去的几天里,我一直在尝试将 Dropzone 实现到我的表单中,但到目前为止,我没有运气弄清楚如何让它上传并仅在单击提交按钮时才处理图像。因此,我决定来这里向你们寻求帮助。
我制作了一个示例代码结构,因此您只能查看它的必要性。现在,我认为当我将一些图片放入拖放区并单击按钮时,它应该会从控制器触发collect_input功能。但是我不知道如何处理文件等。所以,我想我要求的是如何处理表单中的文件的提示/解决方案,例如将它们保存到文件夹并向数据库添加条目。
我将发布下面的代码,如果你们中的任何人有任何提示或解决方案,请与我分享。我要提前感谢大家阅读本文和回复。顺便说一句,我在CodeIgniter工作。
[下载整个项目(css,js和php)http://puu.sh/5eqLc.zip

htdocs/application/controllers/test.php

<?php
class Test extends CI_Controller {
    public function __construct() {
        parent::__construct();

    }
    public function index() {
        $this->load->view('test_view');
    }
    public function collect_input() {
    }
}

htdocs/application/controllers/test_view.php

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta charset="utf=8">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" type="text/javascript"></script>                      
            <!--    Load DropZone        -->
            <link href="<?php echo base_url(); ?>/css/basic.css" type="text/css" rel="stylesheet" />
            <link href="<?php echo base_url(); ?>/css/dropzone.css" type="text/css" rel="stylesheet" />
            <script src="<?php echo base_url(); ?>js/dropzone.min.js"></script>
            <script type="text/javascript">
                jQuery(document).ready(function() 
                {
                    var myDropzone = new Dropzone("div#myId", { url: "file-upload"});
                });
            </script>
            <script type="text/javascript">
            Dropzone.options.myId = {
              // Prevents Dropzone from uploading dropped files immediately
              autoProcessQueue: false,
              init: function() {
                var submitButton = document.querySelector("#add")
                    myDropzone = this; // closure
                submitButton.addEventListener("click", function() {
                  myDropzone.processQueue(); // Tell Dropzone to process all queued files.
                });
                // You might want to show the submit button only when 
                // files are dropped here:
                this.on("addedfile", function() {
                  // Show submit button here and/or inform user to click it.
                });         
              }
            };
            </script>
        </head>
        <body>
            <?php echo form_open('test/collect_input'); ?>
                <!-- One of the many inputs of my form -->
                <select id="list_type">
                   <option value="-1">Chooose a type</option>
                   <option value="1">&gt;&gt;Type A</option>
                   <option value="2">&gt;&gt;Type B</option>
                </select>
                <!--        Dropzone         -->
                <div id="myId" class="dropzone"></div>  
                <!-- Submit button-->
                <input type="submit" id="add" name="add" value="Add!!">
        </form>
    </body>
</html>

您可以提供自己的网址来上传文件。目前我只是将其设置为'someurl'

Dropzone.autoDiscover = false;
jQuery(document).ready(function() {
  var myDropzone = new Dropzone("#myId", { 
    url: 'someurl',
    autoProcessQueue:false
  });
  $('#add').on('click',function(e){
    e.preventDefault();
    myDropzone.processQueue();  
  });   
});
<script>
$(document).ready(function () {
    Dropzone.options.dropzoneForm = {
        autoProcessQueue: false
    };
});
function processEvent() { // call this to trigger the upload
    $('#dropzoneForm').get(0).dropzone.processQueue();
}
</script>
<form action="/your-post-url-here" method="post" enctype="multipart/form-data" class="dropzone" id="dropzoneForm"></form>