I have below code that displays uploaded images dynamically, it's working properly.
$(function () { $("#fileupload").change(function () { if (typeof (FileReader) != "undefined") { var preview = $("#preview"); var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/; $($(this)[0].files).each(function () { var file = $(this); if (regex.test(file[0].name.toLowerCase())) { var reader = new FileReader(); reader.onload = function (e) { var img = $("<img />"); img.attr("style", "height: 180px; width: 180px; margin-left:10px; margin-top: 40px;"); img.attr("src", e.target.result); img.attr("id", "imgUserDisplay"); preview.append(img); $('#saveImage').on({ 'click': function () { var photodisplay = $("#photodisplay"); var photoimg = $(' <img />'); photoimg.attr("style", "height: 180px; width: 180px; margin-top: 40px;cursor:pointer;"); photoimg.attr("id", "photoUserDisplay"); photoimg.attr("class", "margin"); photoimg.attr("data-target", "#commentDiv"); photoimg.attr("data-toggle", "modal"); photoimg.attr("src", e.target.result); if (!(photoimg.attr("src") == null || photoimg.attr("src") == '')) { $('#OnlyPost').attr("style", "display:none;"); $('#ImagePost').removeAttr("style"); $('#imgModalDisplay').attr("src", e.target.result); photodisplay.append(photoimg); $('#fileupload').val(''); $("#textareaimg").val(''); preview.html(""); } else { alert("Un-handeled Exception Caught"); } } }); } reader.readAsDataURL(file[0]); } else { alert(file[0].name + " is not a valid image file."); preview.html(""); return false; } }); } else { alert("This browser does not support HTML5 FileReader."); } }); }); HTML
Used this meta tags to control cache , but still no desired result
<meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="expires" content="0" /> In this code the uploaded images are displayed in different divs which works perfectly. The issue is that the function displays the images which the user uploaded earlier (cache problem?) in all div which are appended in same array. Is there any way to clear the array (cache?) each time you call this particular function? I need help in understanding the syntax. HTML can be provided if necessary.