您的位置:首页 > Web前端 > JavaScript

js实现图片上传并即时显示

2016-12-12 16:13 671 查看
原地址http://www.jb51.net/article/76651.htm

HTML代码

<label for="thumbnail" class="col-md-3 control-label">缩略图</label>
<div class="col-md-6">
<input type="file" class="form-control" id="thumbnail" name="thumbnail">
</div>


JS代码

<script>
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}

$('#thumbnail').change(function() {
var eImg = $('<img />');
eImg.attr('src', getObjectURL($(this)[0].files[0])); // 或 this.files[0] this->input
$(this).after(eImg);});
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: