9

I want to use drop zone drag and drop multiple file up-loader. the thing it does is automatically uploads file to server using Ajax. But I want it to perform another function. I want that whenever I select a file it should create a <input type="file" name="file1"><input type="file" name="file2"> and so on with every field holding that file. so , when at the end i click on submit button. then I should be able to upload it manually.


The scenario is that i am going to upload products. by drag and drop i will be uploading pictures of the products. I know this picture can be uploaded and values can be saved to database but, for the time i have not submitted the form there is no product id yet. and there is field in images tab where we enter the product id. any suggestion to achieve this.

prabhjot
  • 394
  • 2
  • 6
  • 16

1 Answers1

18

You should set autoProcessQueue parameter to false.

You can do this way :

HTML - Add button

<form action="your_action" class="dropzone" id="your_form_id">
     <div class="fallback">
         <input name="file" type="file" />
     </div>
</form>

<button type="button" id="btn_upload">Upload</button>

JavaScript - set autoProcessQueue to false, add click event on the button ID and fire the processQueue event to upload the file

Dropzone.options.your_form_id = {
    autoProcessQueue: false,

    init: function (e) {

        var myDropzone = this;

        $('#btn_upload').on("click", function() {
            myDropzone.processQueue(); // Tell Dropzone to process all queued files.
        });

        // Event to send your custom data to your server
        myDropzone.on("sending", function(file, xhr, data) {

            // First param is the variable name used server side
            // Second param is the value, you can add what you what
            // Here I added an input value
            data.append("your_variable", $('#your_input').val());
        });

    }
};
John
  • 4,711
  • 9
  • 51
  • 101
  • i am not just uploading files. i have to store their address to database too. when i submit the form how it will be done. – prabhjot Aug 26 '16 at 12:31
  • 2
    What is the problem ? In your html form you have your action and you can do what you want server side (store to database, compress etc...) – John Aug 26 '16 at 12:36
  • So you want to send to your server the product id inside the input text that you have in your picture tab. Right ? – John Aug 26 '16 at 12:44
  • like, when adding a new product i have to give description and all other things, i have to upload pictures too and save the filename in a table along with a field to put the product id i will get after adding the product. – prabhjot Aug 29 '16 at 04:30
  • table1 `p_id name cost etc` table2 `id image p_id` (p_id is just AI field) – prabhjot Aug 29 '16 at 04:31
  • I edited my post with the sending event. You can add your input field value in the second parameter inside the data.append() method – John Aug 29 '16 at 07:36
  • @John, how can I do to intercept the file in the server. Im using C# .Net Core, do you know? – Matias Novillo Dec 02 '22 at 10:47