显示从文件选择器中选择的图像的图像缩略图
Display image thumbnail of image selected from filepicker
我使用一个常规的HTML文件选择器输入表单项来允许我的用户选择文件。我想给他们看一个他们选择的文件的缩略图,因为他们需要选择很多图像,这可能会让人困惑。
是否有人知道是否可以在不上传到服务器的情况下显示他们选择的图像?如果是,如何?
如果有人能给我指一个帖子,或者给我一个如何做到这一点的想法,我将不胜感激。
<input type="file" name="image" accept="image/*">
<!-- Create an <img> element to preview the image -->
<img id="image-preview" width="320">
<!-- The <input> can go anywhere in your page or <form>. Note the "onchange" attribute -->
<input type="file" name="image" accept="image/*" onchange="handleFileChange">
请确保以下JavaScript位于上述<img>
和<input>
元素之后。您可以将其放入页面末尾加载的.js
文件中,也可以放入页面末尾的<script>
元素中。
// Select the preview image element
const imgElement = document.getElementById('image-preview');
function handleFileChange(e) {
// If no file was selected, empty the preview <img>
if(!e.target.files.length) return imgElement.src = '';
// Set the <img>'s src to a reference URL to the selected file
return imgElement.src = URL.createObjectURL(e.target.files.item(0))
}
这应该会让你开始
http://jsfiddle.net/41go76g4/
基本上,您使用javascript中的fileAPI读取文件,并将返回的blob设置为img标记的dataURL。
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('fileInput').addEventListener('change', handleFileSelect, false);
参考:http://www.html5rocks.com/en/tutorials/file/dndfiles/
相关文章:
- 当页面包含指向一个图像的多个链接时,fancyBox会复制缩略图辅助图像
- 用较大的图像交换缩略图
- 不应选择缩略图优先图像
- 为缩略图库和较大图像库添加下一个和上一个按钮
- 缩略图图像滑块
- 图像缩略图库中的“下一个”和“上一个”按钮中的“问题”
- 图像库缩略图滑块
- 缩略图库的XML数据图像绑定
- 在缩略图库中显示图像
- 缩略图单击时图像未以模式显示
- JS照片库.使大图像显示和缩略图可单击
- 尝试显示使用 php 和数据库检索的缩略图中的全尺寸图像
- 如何使用jQuery单击其缩略图时显示主图像
- JS-点击缩略图将显示正确的图像
- 动态加载的缩略图显示其他缩略图的全尺寸图像,而不是自己的缩略图(使用javascript悬停效果)
- ExtJS FormPanel,文件上传旁边有图像缩略图
- 显示从文件选择器中选择的图像的图像缩略图
- Jquery缩略图库正在更改所有图像
- 基于缩略图在路径中循环浏览图像
- 如何使用javascript在缩略图中移动多个图像(从phpmyadmin检索)