在我的HTML表单中,我有输入字段和类型文件,例如:
<input type="file" multiple>
然后我通过单击该输入按钮选择多个文件。现在我想在提交表单之前显示所选图像的预览。如何在HTML 5中做到这一点?
HTML5附带File API spec,它允许您创建允许用户在本地与文件交互的应用程序;这意味着您可以加载文件并在浏览器中呈现它们,而无需实际上传文件。 File API的一部分是FileReader接口,它允许Web应用程序异步读取文件的内容。
这是一个快速示例,它使用FileReader
类将图像作为DataURL读取,并通过将图像标记的src
属性设置为数据URL来呈现缩略图:
HTML代码:
<input type="file" id="files" />
<img id="image" />
JavaScript代码:
document.getElementById("files").onchange = function () {
var reader = new FileReader();
reader.onload = function (e) {
// get loaded data and render thumbnail.
document.getElementById("image").src = e.target.result;
};
// read the image file as a data URL.
reader.readAsDataURL(this.files[0]);
};
这是一篇关于using the File APIs in JavaScript的好文章。
下面的HTML示例中的代码段过滤掉用户选择的图像,并将所选文件呈现为多个缩略图预览:
function handleFileSelect(evt) {
var files = evt.target.files;
// 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 style="height: 75px; border: 1px solid #000; margin: 5px" 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);
<input type="file" id="files" multiple />
<output id="list"></output>
这里我用jQuery使用FileReader API。
Html标记:
<input id="fileUpload" type="file" multiple />
<div id="image-holder"></div>
jQuery的:
在jQuery代码中,首先我检查文件扩展名。即要处理的有效图像文件,然后将检查浏览器是否支持FileReader API是否则只处理其他显示尊重的消息
$("#fileUpload").on('change', function () {
//Get count of selected files
var countFiles = $(this)[0].files.length;
var imgPath = $(this)[0].value;
var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
var image_holder = $("#image-holder");
image_holder.empty();
if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
if (typeof (FileReader) != "undefined") {
//loop for each file selected for uploaded.
for (var i = 0; i < countFiles; i++) {
var reader = new FileReader();
reader.onload = function (e) {
$("<img />", {
"src": e.target.result,
"class": "thumb-image"
}).appendTo(image_holder);
}
image_holder.show();
reader.readAsDataURL($(this)[0].files[i]);
}
} else {
alert("This browser does not support FileReader.");
}
} else {
alert("Pls select only images");
}
});
Detailed Article: How to Preview Image before upload it, jQuery, HTML5 FileReader() with Live Demo
function handleFileSelect(evt) {
var files = evt.target.files;
// 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 style="height: 75px; border: 1px solid #000; margin: 5px" 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);
<input type="file" id="files" multiple />
<output id="list"></output>
对于背景图像,请务必使用url()
node.backgroundImage = 'url(' + e.target.result + ')';