How add a seperate input field with each selected image
How can i add a separate custom field with each selected image. What i want to do is to upload a description with each image separately so that on server side the corresponding image can be stored with its description. since i don't know how many images the user may select so i have to generate the input fields on the go only.
To summarize, when i select N no of images , N number of Thumbnails and N no of inputs should come up beside them and each image should be uploaded with its own input field only. is there any simple way to do it rather than uploading every images and then showing the user list of images and getting the descriptions
I have the same question, looked around the issues and couldn't find an answer.
I had the same problem and needed the same thing, so I'll share the solution that I've implemented. You should make your dropzone listen to the sending(file, xhr, formData) event. Events are well explained at dropzonejs' page (http://www.dropzonejs.com/#toc_8).
Maybe your code would be something like this:
Dropzone.options.myAwesomeDropzone = {
init: function() {
this.on("sending", function(file, xhr, formData) {
formData.append("name", value);
});
}
}
If you have any doubt about which values you can append to formData, you should read this: https://developer.mozilla.org/en-US/docs/Web/API/FormData
And that's it! I hope it's useful :)
I really don't understand how to have a separate input for each image upload. I have tried everything I can find / think of. I am using the boostrap example from the Dropzone.js website (http://www.dropzonejs.com/bootstrap.html). Here is the HTML
<div id="actions" class="row">
<div class="col-lg-7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Add files...</span>
</span>
<button type="submit" class="btn btn-primary start">
<i class="glyphicon glyphicon-upload"></i>
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="glyphicon glyphicon-ban-circle"></i>
<span>Cancel upload</span>
</button>
</div>
<div class="col-lg-5">
<!-- The global file processing state -->
<span class="fileupload-process">
<div id="total-progress" class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
<div class="progress-bar progress-bar-success" style="width: 0%;" data-dz-uploadprogress></div>
</div>
</span>
</div>
</div>
<div class="table table-striped" class="files" id="previews">
<div id="template" class="file-row">
<!-- This is used as the file preview template -->
<div>
<span class="preview">
<img data-dz-thumbnail /></span>
</div>
<div>
<p class="name" data-dz-name></p>
<strong class="error text-danger" data-dz-errormessage></strong>
</div>
<div>
<p class="tags" data-dz-tags></p>
<input type="text" id="tags" name="tags" />
</div>
<div>
<p class="size" data-dz-size></p>
<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
<div class="progress-bar progress-bar-success" style="width: 0%;" data-dz-uploadprogress></div>
</div>
</div>
<div>
<button class="btn btn-primary start">
<i class="glyphicon glyphicon-upload"></i>
<span>Start</span>
</button>
<button data-dz-remove class="btn btn-warning cancel">
<i class="glyphicon glyphicon-ban-circle"></i>
<span>Cancel</span>
</button>
<button data-dz-remove class="btn btn-danger delete">
<i class="glyphicon glyphicon-trash"></i>
<span>Delete</span>
</button>
</div>
</div>
</div>
When I open the page, the input box displays exactly as I would expect for each image upload row. I have also added the following to the javascript.
myDropzone.on("sending", function (file, xhr, formData) {
formData.append("tags", document.getElementById('tags').value);
// Show the total progress bar when upload starts
document.querySelector("#total-progress").style.opacity = "1";
// And disable the start button
file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");
});
But no matter what I try, when I click the "Start Upload" button, all I can ever get back from the document.getElementById("tags").value is the value of the first input box. No matter how many files are being uploaded, each time it hits the "sending" function, it gives me the value from that first input box.
What am I missing?? This seems like a common question but I have yet to see an actual answer for it.
Thanks, Keith
Had the same problem. Maybe it's because, if there were more than one file, the page would have several input with the same id=tags? And getElementById was supposed to return the first, though it's non-standard to have several id with the same value?
Update: After some reading I found out that in the docs it says 'You can access the HTML of the file preview in any of the events with file.previewElement'.
So file.previewElement.querySelector('input#tags') should get to the very input element belong to this file.
It's been a long time so I think you might have made it work already. Just ignore me. :)
@keiffer6 did you find the answer for you question? Im having the same issue
This is not a problem of Dropzone. It definitely SHOULD be mentioned in WIKI as this is something completely basic and many developers need it. It has to do with how HTML protocol sends data. If you have several inputs with identical name, it will send only the last one and discard all the previous ones.
Therefore, your problem is input's name. IDs should be always unique anyway, so you can make sure they are by appending current time to their name. E. g. var unique_field_id = new Date().getTime(); See it in my code.
The next task (in my case) was to dynamically create input fields for both image's title and description. You can use your custom template hardcoded in your view (MVC-wise) or you can also do it dynamically in addedfile phase of Dropzone's processing mechanism.
Then, in sending phase you need to get values of those fields (either dynamically created ones or hardcoded ones) and append them to the formData being sent to your server. Please see my solution. I have tested it and it works.
Please note the parameters sent to the server will include both correct and wrong title but you just grab the good one and discard the wrong one. I am sure this can be further optimized, so you can give it a go. But as a solution it works.
// 'scope' is all files
this.on("addedfile", function (file) {
var unique_field_id = new Date().getTime();
title = file.title == undefined ? "" : file.title;
file._titleLabel = Dropzone.createElement("<p>Title:</p>")
file._titleBox = Dropzone.createElement("<input id='"+file.name+unique_field_id+"_title' type='text' name='title' value="+title+" >");
file.previewElement.appendChild(file._titleLabel);
file.previewElement.appendChild(file._titleBox);
description = file.description == undefined ? "" : file.description;
file._descriptionLabel = Dropzone.createElement("<p>Description:</p>")
file._descriptionBox = Dropzone.createElement("<input id='"+file.name+unique_field_id+"_desc' type='text' name='description' value="+description+" >");
file.previewElement.appendChild(file._descriptionLabel);
file.previewElement.appendChild(file._descriptionBox);
});
// 'scope' is file currently being sent only, send our custom fields along
this.on("sending", function (file, xhr, formData) {
title = file.previewElement.querySelector("input[name='title'");
description = file.previewElement.querySelector("input[name='description'");
// format of this depends on your paramName config. Mine was called image
formData.append("image[title]", $(title).val());
formData.append("image[description]", $(description).val());
});
What do I mean by saying it appends also the wrong title and description? Because of our constructed code in append phase, it will take the last value entered there - and that's your second image's description - in case you're uploading two images. But you can simply ignore this data or remove it from incoming data with regexp.
In case of Rails your log will show this (see the difference between image["title"] and title):
Parameters: {"image"=>{"title"=>"Correct title", "description"=>"correct description", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f35fc81fb58 @tempfile=#<Tempfile:/tmp/RackMultipart20170114-12003-1ypqlqs.jpg>, @original_filename="a07b2d172
45.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image[image]\"; filename=\"a07b2d17245.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "utf8"=>"✓", "authenticity_token"=>"BxCFWUZzA9ZaHNM4oN93nGN1...", "commit"=>"Upload", "title"=>"wrong - it appends the title of the next image", "description"=>"wrong - it appends the description of the next image this time", "gallery_id"=>"42"}
So in your controller you need to access the correct data with (in case of my parameter list) image["title"] and not just title, which would contain the wrong version of second image's desc.
Rails-only warning - to make this work, uploadMultiple must be set to false in DZ configuration, otherwise it constructs a really weird POST using arrays.
As always, watch for your server's logs. This should be closed now.
@technodrome Wonderful solution! Thanks a lot!
Thanks @technodrome for your thorough answer! I'll add it to the wiki when I find some time.
@technodrome Thanks for the nice detailed answer, you are awesome!
Thanks @technodrome for this great share 💯
Gladly :)
Thanks @technodrome. I took me one day to find this solution!
How I can update the description field again after the queue is processed?
@technodrome I tried your solution, but the value is always empty. Any idea what I might do wrong?
Dropzone need something like a meta that can be passed -other upload js also have kind of extra. For example -> https://uppy.io/docs/dashboard/#metaFields