如何在Bootstrap Modal Box中显示图像预览?

时间:2017-05-02 06:28:34

标签: javascript jquery html twitter-bootstrap

我已经使用javascript在图像预览内联html之前完成了编码文件上传。但我需要在Bootstrap Modal弹出框中显示图像预览。不是HTML内联。我应该如何更改我的代码?这是我的代码。

<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>

这是我的Javscript代码:

<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;
 }

2 个答案:

答案 0 :(得分:2)

此代码段为您提供了如何显示预览图像的基本知识。在onChange文件期间,读取图像并将src值设置为另一个图像字段。

                          

function readURL(input, id) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#' + id).attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#uploadBtn").change(function () {
        readURL(this, 'photoShowId');
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="uploadBtn" type="file" class="upload" multiple="multiple" accept="image/*" name="browsefile" />
        <div id="upload_prev">
        <img id="photoShowId" src="" width="120" height="150" style="border: 1px solid blue" />
        </div>

答案 1 :(得分:0)

谢谢你们。现在我可以在bootstrap模式中显示预览。这是我的更改代码。

<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>
 <!-- Modal -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
   <div class="modal-dialog" role="document">
     <div class="modal-content">
       <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
           <h4 class="modal-title" id="myModalLabel">Modal title</h4>
       </div>
       <div class="modal-body">
         <div class="image_preview">
           <label class="filelink"></label>
         </div>
       </div>
      <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
         <button type="button" class="btn btn-primary">Save changes</button>
     </div>
    </div>
   </div>
   </div>

这是我的javascript更改。

<script type="text/javascript">
 $(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 = $('#myModal').closest("div")
        .find(".filelink");
      if (!$('#myModal').closest("div")
        .find("img").length) 
        close
        .after(
          $('<img>', {
            src: URL.createObjectURL(files[i])
          }).width('70%').height('70%')
        )
      else
        close.siblings("img").toggle()
     })
   })(i);
 }
   document.getElementById('filename').value = filename;
 }
</script>