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

这种方方式是通过:

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' οnchange="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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值