h5 input type=file 图片预览

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了h5 input type=file 图片预览相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>html5 图片上传预览</title>
<style>
#preview { width: 300px; height: 300px; overflow: hidden; } #preview img { width: 100%; height: 100%; }
</style>
<script src="jquery.min.js"></script>
<script type="text/javascript">
function preview1(file) {
var img = new Image(),
url = img.src = URL.createObjectURL(file)
var $img = $(img)
img.onload = function() {
URL.revokeObjectURL(url)
$(‘#preview‘).empty().append($img)
} }
// function preview2(file) {
// var reader = new FileReader()
// reader.onload = function(e) {
// var $img = $(‘<img>‘).attr("src", e.target.result)
// $(‘#preview‘).empty().append($img)
// }
// reader.readAsDataURL(file)
}
$(function() {
$(‘[type=file]‘).change(function(e) {
var file = e.target.files[0]
preview1(file)
})
})
</script>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post">
<input type="file" name="imageUpload"/>
<div id="preview" style="width: 300px;height:300px;border:1px solid gray;"></div>
</form>
</body>
</html>

以上是关于h5 input type=file 图片预览的主要内容,如果未能解决你的问题,请参考以下文章

input[type='file']样式美化及实现图片预览

前端实现input[type='file']上传图片预览效果

H5图片预览压缩上传

H5图片预览压缩上传

h5图片上传预览

H5压缩图片上传(FileReader +canvas)