I have done coding file upload before image preview inline html with javascript. But i need to show image preview in Bootstrap Modal Popup Box. Not in HTML Inline. How should i change my code? Here is my codes.
<input id="uploadBtn" type="file" class="upload" multiple="multiple" accept="image/*" name="browsefile" style="display: none !important;" />
<input type="button" value="ファイル追加" onclick="document.getElementById('uploadBtn').click();" style="float: right;"/>
<input id="filename" type="hidden" />
<br>
<div id="upload_prev"></div>
<div style="clear:both;"></div>
Here is my Javscript Code:
<script>
$(document).on('click','.close',function(){
$(this).parents('span').remove();
})
document.getElementById("uploadBtn").onchange = function() {
document.getElementById("uploadFile").value = this.value;
};
document.getElementById('uploadBtn').onchange = uploadOnChange;
function uploadOnChange() {
var filename = this.value;
var lastIndex = filename.lastIndexOf("\\");
if (lastIndex >= 0) {
filename = filename.substring(lastIndex + 1);
}
var files = $('#uploadBtn')[0].files;
for (var i = 0; i < files.length; i++) {
(function(i) {
$("#upload_prev").append('<div><span><br><div class="col-md-10"><span class="uploadFiles">' + '<a href="" data-toggle="modal" data-target="#myModal">' + files[i].name + '</a>' + '</span><br><label class="filelink"></label></div><div class="col-md-2"><p class="close" style="font-size: 13pt;">削除</p><br></div></span></div>');
$("#upload_prev a:contains(" + files[i].name + ")")
.on("click", function(e) {
e.preventDefault();
var close = $(this).closest("div")
.find(".filelink");
if (!$(this).closest("div")
.find("img").length)
close
.after(
$('<img>', {
src: URL.createObjectURL(files[i])
}).width('50%').height('50%')
)
else
close.siblings("img").toggle()
})
})(i);
}
document.getElementById('filename').value = filename;
}