I've made a file upload with file previews using html5+file reader and it works fine except that old files the user selected gets destroyed from input file field and if the user select in a new single browse click.
Here's the js fiddle https://jsfiddle.net/sco3pq3b/
html
<p> UPLOAD </p>
<input type="file" multiple="yes" name="photo[]" id="photos">
<div id="preview"></div>
js
$("#photos").change(function(){
var fileList = this.files;
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
for(var i = 0; i < fileList.length; i++){
if (regex.test(fileList[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').append('<div class="imgLinkWrap"><a class="fancybox" href="' + e.target.result +'">'+fileList[0].name.toLowerCase()+'</a></div>');
}
console.log(fileList[i]);
reader.readAsDataURL(fileList[i]);
} else {
alert(file[0].name + " is not a valid image file.");
$('#preview').html("");
return false;
}
}
});
Is there anyway to preserve the old files after a new browse file click without using any plugin or ajax?