JSP 上传图片之前先在页面生成预览

2021-09-22 10:47:10 浏览数 (2)

代码语言:javascript复制
JSP 页面代码:

    <form action="#" name="uploadForm" method="post" enctype="multipart/form-data">  

     <input type="file" name="upload" id="upload" onchange="loadImageFile(event)">

            <img id="image" src="" >

            <input type="submit" value="上传">

    </form>  


JS 代码:

    function loadImageFile(event)

    {

            var image = document.getElementById('image');

            image.src = URL.createObjectURL(event.target.files[0]); 

    };


</pre> 

0 人点赞