Home  >  Article  >  php教程  >  Input file upload image preview function example code

Input file upload image preview function example code

高洛峰
高洛峰Original
2016-12-09 09:55:191287browse

Input file upload image preview is actually very simple, but it feels amazing if you have never done it before. Today I will take off its mysterious veil. In fact, the principle is really simple. Everyone can understand it through a piece of code below.

The specific code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="jquery.js"></script>
</head>
<body>
<input type="file" multiple id="inputs"/> //multiple(多文件上传)
<div id=&#39;dd&#39;></div>
<script>
$(document).ready(function () {
$("#inputs").change(function () {
var fil = this.files;
for (var i = 0; i < fil.length; i++) {
reads(fil[i]);
}
});
});
function reads(fil){
var reader = new FileReader();
reader.readAsDataURL(fil);
reader.onload = function()
{
document.getElementById("dd").innerHTML += "<img src=&#39;"+reader.result+"&#39;>";
};
}
</script>
</body>
<html>

Achieve direct preview of uploaded images, avoiding the redundant process of reading images after submission

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn