HTML5上传图片并预览
一个简易的实现:
<!DOCTYPE html> <html> <head> <title>HTML5上传图片并预览</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script> </head> <body> <h3>请选择图片文件:PNG/JPG/JPEG/SVG/GIF</h3> <form name="form0" id="form0"> <input type="file" name="file0" id="file0" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg"/><br> <img src="" id="img0" style="width: 20rem;height: 15rem;"> </form> <script type="text/javascript"> $("#file0").change(function(){ var objUrl = getObjectURL(this.files[0]) ;//获取文件信息 console.log("objUrl = "+objUrl); if (objUrl) { $("#img0").attr("src", objUrl); } }); function getObjectURL(file) { var url = null; if(window.createObjectURL!=undefined) { 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 ; } </script> </body> </html>
效果:
真正的传送给后台可以用ajax,但是我还不会搭后台
参考链接:
1. https://blog.csdn.net/qq_41485414/article/details/80051845
2. https://blog.csdn.net/x550392236/article/details/76690927
相关推荐
WebVincent 2020-06-06
wusiye 2020-10-23
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...
gufudhn 2020-08-09
nercon 2020-08-01
swiftwwj 2020-07-21
nercon 2020-07-16
饮马天涯 2020-07-05
Lophole 2020-06-28
gufudhn 2020-06-12
csstpeixun 2020-06-11
huzijia 2020-06-09
行吟阁 2020-05-30
qsdnet我想学编程 2020-05-26
gufudhn 2020-05-25
qsdnet我想学编程 2020-05-19
suixinsuoyu 2020-05-15
HSdiana 2020-05-15
PioneerFan 2020-05-15