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

上传时图片时 预览本地图片

2018-03-30 17:44 183 查看
这种方方式是通过:
window.URL.createObjectURL()方法生成的资源URL

bdFile="Blob:http...3a%/asdasd89090786" 
然后直接赋给<img>效果图如下:



前端代码:
<form action="__URL__/index1" enctype="multipart/form-data" method="post" >

图片:<input type='file' id="id2" name='photo' onchange="sc(this)"></br>
<div id="div1"style="width: 50px;height:50px; display: none;">
<img  style="width: 100px;height:100px;" id="img1"src="">
</div>
<input style="display: block;" type="submit" value="提交">

</form>

js代码
<script type="text/javascript">

function sc(res){
//为本地资源生成资源URL
url = window.URL.createObjectURL(document.getElementById("id2").files[0]);     
document.getElementById("img1").src=url;           //再赋给img
if(url)
{
document.getElementById("div1").style.display='block';
}
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js html