如何将图像显示到弹出窗口

时间:2012-02-29 08:19:15

标签: javascript html5 filereader fileapi

我使用readAsdataUrl javascript函数在内存中读取了一个图像。现在,我需要在弹出窗口中显示此图像,当在现有页面中单击按钮时,该窗口将打开。

我该怎么做?有什么方法可以从内存中读取图像然后在新的弹出窗口中显示它?

1 个答案:

答案 0 :(得分:0)

检查一下 - http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files

似乎有你需要的例子:

<style>
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }
</style>

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  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);
</script>

只需用您想要的弹出窗口替换该div