I can preview many images without any problem. But I don't know how I can get the value of the image clicked on the upload section and send it with ajax?
HTML:
<div>
<input type="file" name="file[]" id="images" multiple />
</div>
<ul id="prewiew">
</ul>
JavaScript:
$(document).ready(function () {
$('#images').on('change', function (e) {
//It prints the added pictures one by one on the page with a loop.
var files = e.target.files;
$.each(files, function (i, file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
var template = '<li>' +
'<img src="' + e.target.result + '" width="50" height="50"> ' +
'<label>' + file.name + file.size + '</label> <input type="text" name="title">' +
' <button class="btn btn-sm btn-info upload">Yükle</button>' +
'</li>';
$('#prewiew').append(template);
$('#resim').val('');
};
});
});
$(document).on('click', '.upload', function () {
//How can I receive and transfer data when the button is clicked?
var file = $(this).closest('img');
var data = new FormData();
data.append('ImageFile', file);
$.ajax({
url: '/Home/ImagePost/',
type: 'POST',
data: data,
contentType: false,
processData: false,
success: function () {
},
});
});
Summary: I am previewing multiple images. I want to send the previewed picture by pressing the upload button.