zoukankan      html  css  js  c++  java
  • 上传图片及时预览

    最近写了一个上传图片及时预览的功能,也参考了网上一些人代码,可以支持最新的chrome,FF,Ie,以及Ie6

    HTML

    1     <img id="imgTag" style="height: 100px;" />
    2     <input type="file" onchange="DisplayImage(this,'imgTag')"/>

    Js

    function DisplayImage(fileTag,imgTagId){
        var allowExtention=".jpg.png.gif";
        var extentionArr=fileTag.value.split('.');
        var extention = extentionArr[extentionArr.length-1];
        if(!(allowExtention.indexOf(extention)>-1)){
            alert("Please upload image!");
        }else{
            //for adveced broswer(the newest ie,chrome,ff)
            if(typeof(FileReader)!=="undefined"){
                var reader = new FileReader();
                reader.readAsDataURL(fileTag.files[0]);
                reader.onload = function(e){
                    document.getElementById(imgTagId).setAttribute("src", e.target.result);
                }
            }else{
            //for(ie6)
            document.getElementById(imgTagId).setAttribute("src",fileTag.value);
            }
        }
    }

                                                                       --Ones

  • 相关阅读:
    c++类的知识点(1)
    并查集经典例题分析
    并查集
    bfs-迷宫
    出栈次序--数学归纳法--蓝桥
    九宫重排
    Tomcat详解
    寒假日记-第三天
    寒假日记-第二天(MySQL语句)
    Java学期总结
  • 原文地址:https://www.cnblogs.com/ones/p/4361901.html
Copyright © 2011-2022 走看看