input file上传图片预览,非插件

Input标签
<input  type="file" name="pic" onchange="changepic(this)" multiple="multiple"/>

 JS方法

            function changepic(obj) {
                //console.log(obj.files[0]);//这里可以获取上传文件的name
                var newsrc = getObjectURL(obj.files[0]);
                document.getElementById('show').src = newsrc;
            }
            function getObjectURL(file) {
                var url = null;
                // 下面函数执行的效果是一样的,只是需要针对不同的浏览器执行不同的 js 函数而已
                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;
            }

  

猜你喜欢

转载自www.cnblogs.com/axu92312/p/9120623.html