I am using jQuery-File-Upload plugin for file upload. https://blueimp.github.io/jQuery-File-Upload/
My question is how to add a array of files to input files[]
dynamically on click using JQuery
?
Below is the code, that I am trying but I think something went wrong.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/css/jquery.fileupload.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/css/jquery.fileupload-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/vendor/jquery.ui.widget.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-JavaScript-Templates/3.11.0/js/tmpl.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-load-image/2.19.0/load-image.all.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/javascript-canvas-to-blob/3.14.0/js/canvas-to-blob.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/jquery.iframe-transport.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/jquery.fileupload.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/jquery.fileupload-process.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/jquery.fileupload-image.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/jquery.fileupload-validate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/jquery.fileupload-ui.js"></script>
<div class="container">
<!-- The file upload form used as target for the file upload widget -->
<form id="fileupload" action="https://jquery-file-upload.appspot.com/" method="POST" enctype="multipart/form-data">
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="row fileupload-buttonbar">
<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 on click</span>
</span>
<span class="btn btn-success fileinput-button hide">
<i class="glyphicon glyphicon-plus"></i>
<span>Add files...</span>
<input type="file" name="files[]" multiple>
</span>
<button type="submit" class="btn btn-primary start">
<i class="glyphicon glyphicon-upload"></i>
<span>Start</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="glyphicon glyphicon-ban-circle"></i>
<span>Cancel</span>
</button>
<button type="button" class="btn btn-danger delete">
<i class="glyphicon glyphicon-trash"></i>
<span>Delete</span>
</button>
<input type="checkbox" class="toggle">
<!-- The global file processing state -->
<span class="fileupload-process"></span>
</div>
<!-- The global progress state -->
<div class="col-lg-5 fileupload-progress fade">
<!-- The global progress bar -->
<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="progress-bar progress-bar-success" style="width:0%;"></div>
</div>
<!-- The extended global progress state -->
<div class="progress-extended"> </div>
</div>
</div>
<!-- The table listing the files available for upload/download -->
<div class="table-responsive">
<table role="presentation" class="table table-striped table-condensed table-bordered">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
<th>Size</th>
</tr>
</thead>
<tbody class="files"></tbody>
</table>
</div>
</form>
</div>
<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td style="width:20px !important;">
<span href="{%=file.url%}" class="image_pop" style="cursor:pointer"><span class="preview hid"></span></span>
</td>
<td style="width:100px !important;vertical-align: middle;">
<div style="max-width:250px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">
<span class="name" title="{%=file.name%}">{%=file.name%}</span>
</div>
</td>
<td style="width:100px !important;vertical-align: middle;">
<span class="size">Processing...</span>
</td>
<td>
{% if (!i && !o.options.autoUpload) { %}
<button class="btn btn-primary start" disabled>
Start
</button> {% } %} {% if (!i) { %}
<button class="btn btn-warning cancel">
Cancel
</button> {% } %}
</td>
</tr>
{% } %}
</script>
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td style="width:20px !important;">
<span class="preview">
{% if (file.thumbnailUrl) { %}
<a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
{% } %}
</span>
<td style="width:100px !important;vertical-align: middle;">
<div style="max-width:250px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">
<span class="name" title="{%=file.name%}">{%=file.name%}</span>
</div>
<td style="width:100px !important;vertical-align: middle;">
<span class="size">{%=o.formatFileSize(file.size)%}</span>
</td>
<td>
{% if (file.deleteUrl) { %}
<button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}" {% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}' {% } %}>
Delete
</button>
<input type="checkbox" name="delete" value="1" class="toggle"> {% } else { %}
<button class="btn btn-warning cancel">
Cancel
</button> {% } %}
</td>
</tr>
{% } %}
</script>
<script>
$('.fileinput-button').on('click', function () {
var $json = ["https://images.pexels.com/photos/1124092/pexels-photo-1124092.jpeg?cs=srgb&dl=abstract-art-background-1124092.jpg&fm=jpg",
"https://images.pexels.com/photos/1145357/pexels-photo-1145357.jpeg?cs=srgb&dl=blue-sky-clouds-countryside-1145357.jpg&fm=jpg",
"https://images.pexels.com/photos/861233/pexels-photo-861233.jpeg?cs=srgb&dl=asphalt-car-close-up-861233.jpg&fm=jpg",
"https://images.pexels.com/photos/1174183/pexels-photo-1174183.jpeg?cs=srgb&dl=4k-wallpaper-conifers-environment-1174183.jpg&fm=jpg",
"https://images.pexels.com/photos/1167110/pexels-photo-1167110.jpeg?cs=srgb&dl=adorable-animal-breed-1167110.jpg&fm=jpg",
"https://images.pexels.com/photos/457446/pexels-photo-457446.jpeg?cs=srgb&dl=clouds-fashion-landscape-457446.jpg&fm=jpg",
"https://images.pexels.com/photos/1170594/pexels-photo-1170594.jpeg?cs=srgb&dl=abstract-art-blue-sky-1170594.jpg&fm=jpg",
"https://images.pexels.com/photos/1006293/pexels-photo-1006293.jpeg?cs=srgb&dl=contemporary-desk-eyeglasses-1006293.jpg&fm=jpg"];
$('#fileupload').fileupload({
url: $(this).attr('action'),
dataType: 'json',
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
}).fileupload('add', {
files: $json
});
});
</script>