我已经使用 readAsdataUrl 读取了内存中的图像javascript函数。现在我需要在一个弹出窗口中显示此图像,该窗口在单击现有页面中的按钮时打开。
我该怎么做?有什么方法可以从内存中读取图像,然后将其显示在新的弹出窗口中?
最佳答案
检查这个 - 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 替换为您想要的弹出窗口即可
关于javascript - 如何将图像显示到弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9495659/