1

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.

5
  • use meta tags to prevent caching Commented Aug 6, 2015 at 11:49
  • try to add some parameter with image name like test.jpg?time() this will load image from server not from cache Commented Aug 6, 2015 at 11:50
  • stackoverflow.com/a/15228975/1675954 Commented Aug 6, 2015 at 11:50
  • Can someone suggest how can i clear that array in above JQuery function ? after its almost appended in all div's ? that will be much helpfull :) Commented Aug 6, 2015 at 12:00
  • used manifest.appcache but Still its showing images which are previously uploaded , i think changes are needed in jquery function . Commented Aug 6, 2015 at 12:04

1 Answer 1

1

Your elements share the same static IDs

 img.attr("id", "imgUserDisplay"); 

Remove it or make it dynamic as well. Sharing the same ID with multiple elements can cause unforeseen behaviour.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you suggest an article to create dynamic id's ?
there are lots of libraries that can do it for you: e.g. link img.attr("id", _.uniqueId("imgUserDisplay"));
preview.append(img) not working after switching to img.attr("id", _.uniqueId("imgUserDisplay"));
Have you included the lodash library? e.g. <script src="://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
preview.append(img) started Working :) , Now lets see //var_name.attr("id", _.uniqueId("//some_id")); gives me expected results .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.