如何使用用户图像更新<img/>标记

时间:2014-10-23 19:01:51

标签: javascript jquery html css twitter-bootstrap

我正在使用自举网页,想要拍摄用户输入的图像并在模态中显示预览。模态包括用户文本,一切正常。

模态的代码如下(截至图片):

<div class="modal modal-wide fade" id="previewModal" tabindex="-1" role="dialog" aria-labelledby="previewModalTitle" aria-hidden="true">
<div class="modal-dialog modal-lg">
    <div class="modal-content">
         <div class="modal-header">
             <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                 <h4 class="modal-title" id="previewModalTitle">Your Newstream Preview</h4>
         </div>
          <div class="modal-body">
              <div id="previewNewstreamImage" class="carousel slide">
                 <div class="carousel-inner">       
                    <div class="item active">
                        <img id='previewNewstreamImageFile' src="http://CrowdNe.ws/assets/img/newstreamPosters/droneSunset.jpg" >

此轮播正在更新:

   <div id="myCarousel" class="carousel slide">
   <div class="carousel-inner">       
    <div class="item active">
     <img src="picture here" >  <div class="container"><!-- container -->
      <div align="center"> 
       <div class="findStream">
        <h2  style="color:#FFFFFF;">Upload  Main Image</h2>
         <div align="center">
         <form role="form">
     <div class="form-group" style="color:#FFFFFF;"> <!--form group  -->
       <input type='file' id="myFile" onChange="readURL(this)" />
     </div><!--/div row.newstreamImage --> 
   </form>
      </div>  
              <p  style="color:#FFFFFF;">Images are optional but <em>highly</em> recommended. <br>
                Best image dimension is 800px by 400px, jpg or png. File sizes less than 500k.</p>

       </div>
      </div>
     </div>
    </div>

   </div>
   <!-- /.carousel -->
 </div>
</div>

然后像这样调用JavaScript:

var picture = document.getElementById("myFile").value;
        document.getElementById("previewNewstreamImageFile").src = picture;

谢谢!

1 个答案:

答案 0 :(得分:0)

  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                            '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

归因:http://www.html5rocks.com/en/tutorials/file/dndfiles/