Jquery Image Upload Preview
Pequeno código para fazer upload com pré-visualização de imagem.
Código completo disponível no CodePen: https://codepen.io/fernandokosh/pen/wyKjRR
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<body>
<form id="form1" runat="server">
<input type='file' id="file-input" />
<div id='img_contain'>
<img id="image-preview" align='middle'src="http://www.clker.com/cliparts/c/W/h/n/P/W/generic-image-file-icon-hi.png" alt="your image" title=''/>
</div>
</form>
</body>
CSS:
body{
background-color:#add8e6;
}
#img_contain{
border:1px solid grey;
margin-top:10px;
width:520px;
}
#file-input{
margin-left:7px;
padding:10px;
background-color:gray;
}
#image-preview{
height:320px;
width:auto;
display:block;
margin-left: auto;
margin-right: auto;
padding:5px;
}
JQuery:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#image-preview').attr('src', e.target.result);
$('#image-preview').hide();
$('#image-preview').fadeIn(650);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#file-input").change(function() {
readURL(this);
});
Código completo disponível no CodePen: https://codepen.io/fernandokosh/pen/wyKjRR
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<body>
<form id="form1" runat="server">
<input type='file' id="file-input" />
<div id='img_contain'>
<img id="image-preview" align='middle'src="http://www.clker.com/cliparts/c/W/h/n/P/W/generic-image-file-icon-hi.png" alt="your image" title=''/>
</div>
</form>
</body>
CSS:
body{
background-color:#add8e6;
}
#img_contain{
border:1px solid grey;
margin-top:10px;
width:520px;
}
#file-input{
margin-left:7px;
padding:10px;
background-color:gray;
}
#image-preview{
height:320px;
width:auto;
display:block;
margin-left: auto;
margin-right: auto;
padding:5px;
}
JQuery:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#image-preview').attr('src', e.target.result);
$('#image-preview').hide();
$('#image-preview').fadeIn(650);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#file-input").change(function() {
readURL(this);
});
Comentários
Postar um comentário