上传图片并浏览

Posted 守候幸福

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传图片并浏览相关的知识,希望对你有一定的参考价值。

<input type="file" id="fileElem" multiple accept="image/*"  onchange="handleFiles(this)">
<div id="fileList" style="width:200px;height:200px;"></div>

 

<script>
    window.URL = window.URL || window.webkitURL;
    var fileElem = document.getElementById("fileElem"),
    fileList = document.getElementById("fileList");
    function handleFiles(obj) {
        var files = obj.files,
        img = new Image();
        if(window.URL){
            //File API
            alert(files[0].name + "," + files[0].size + " bytes");
            img.src = window.URL.createObjectURL(files[0]); //创建一个object URL,并不是你的本地路径
            img.width = 200;
            img.onload = function(e) {
                window.URL.revokeObjectURL(this.src); //图片加载后,释放object URL
            }
            fileList.appendChild(img);
        }else if(window.FileReader){
            //opera不支持createObjectURL/revokeObjectURL方法。我们用FileReader对象来处理
            var reader = new FileReader();
            reader.readAsDataURL(files[0]);
            reader.onload = function(e){
                alert(files[0].name + "," +e.total + " bytes");
                img.src = this.result;
                img.width = 200;
                fileList.appendChild(img);
            }
        }else{
            //ie
            obj.select();
            obj.blur();
            var nfile = document.selection.createRange().text;
            document.selection.empty();
            img.src = nfile;
            img.width = 200;
            img.onload=function(){
                alert(nfile+","+img.fileSize + " bytes");
            }
            fileList.appendChild(img);
        }
    }
</script>

 

以上是关于上传图片并浏览的主要内容,如果未能解决你的问题,请参考以下文章

3种上传图片并实现预览的方法

我可以将多张图片上传到浏览器并使用 Javascript 将其压缩到一个文件中吗?

CANVAS运用-对图片的压缩上传(仅针对移动浏览器)

图片上传组件开发

谷歌浏览器无法上传图片,怎么办

C# 怎么实现上传图片到数据库 代码