用html的file标签就能实现图片上传前预览,就是通过file标签和js的FileReader接口,把选择的图片文件调用readAsDataURL方法,把图片数据转成base64字符串形式显示在页面上。
<input type="file" id="xdaTanFileImg" onchange="xmTanUploadImg(this)" accept="image/*" /> <img id="xmTanImg" /> <script type="text/javascript"> if (typeof FileReader == 'undefined') { document.getElementById("xmTanDiv").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>"; }
function xmTanUploadImg(obj) { var file = obj.files[0];
console.log(obj); console.log(file); console.log("file.size = " + file.size);
var reader = new FileReader();
reader.onloadstart = function (e) { console.log("开始读取...."); } reader.onprogress = function (e) { console.log("正在读取中...."); } reader.onabort = function (e) { console.log("中断读取...."); } reader.onerror = function (e) { console.log("读取异常...."); } reader.onload = function (e) { console.log("成功读取....");
var img = document.getElementById("xmTanImg"); img.src = e.target.result; }
reader.readAsDataURL(file) } </script>
|