我可以使用javascript fileReader读取上传的图片,但是如何在jquery中读取上传的图片,以便在上传之前预览图片?
发布于 2013-10-29 12:08:37
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#previewHolder').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
} else {
alert('select a file to see preview');
$('#previewHolder').attr('src', '');
}
}
$("#filePhoto").change(function() {
readURL(this);
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<br/><br/>
<input type="file" name="filePhoto" value="" id="filePhoto" class="required borrowerImageFile" data-errormsg="PhotoUploadErrorMsg">
<br/><br/>
<img id="previewHolder" alt="Uploaded Image Preview Holder" width="250px" height="250px" style="border-radius:3px;border:5px solid red;"/>
发布于 2018-01-11 16:48:00
使用javascript
<div class="form-group">
<label for="password" class="form-group">upload Image</label>
<input id="image" type="file" name="image" onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])" required="required">
<img id="blah" width="50" height="50" />
</div>
发布于 2020-06-15 15:01:54
To Preview 在使用jquery上传之前预览图像
创建一个事件onchange,该事件将在选择任何图像时触发,loadImg()函数可用于在帧内加载图像。
现场示例:
function loadImg(){
$('#frame').attr('src', URL.createObjectURL(event.target.files[0]));
}<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<input type="file" accept="image/" onchange="loadImg()"><br/>
<img id="frame" width="100px" height="100px"/>
</body>
</html>
https://stackoverflow.com/questions/18694437
复制相似问题