【H5】 图片拖入浏览器readAsDataURL与createObjectURL处理图片:

【H5】 外部图片拖入浏览器:

效果图如下:

在这里插入图片描述

  • const file = new FileReader();//读到文件对象的信息
  • file.readAsDataURL(oFile);//读取到文件所以的url,需要转入对象;

方法一,实现代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
    html,body{
        width:100%;
        height:100%;
        display: flex;
        justify-content:center;
        align-items: center;
    }
    body{
        background-color: #ddd;
    }
    #box{
        width:80%;
        height:200px;
        background-color: deeppink;
    }
</style>
</head>
<body>
    
    <div id="box"></div>
    <script>

        const box = document.getElementById('box');
        box.ondragover = function(e){   //拖拽物在box内持续触发
            e.preventDefault(); //阻止默认事件
            e.stopPropagation();//阻止冒泡事件
            return false;
        }      
        box.ondrop = function(e){   //拖拽在box抬起触发
            const dt = e.dataTransfer;
            const oFile = dt.files.item(0);
            const file = new FileReader();//读到文件对象的信息
            file.readAsDataURL(oFile);//读取到文件所以的url,需要转入对象;
     
            file.onload = function( e ){    //onload图片加载完成后触发的事件
                console.log( file.result )
                if( /image/.test(file.result) ){    //判断拖拽的是否是图片
                    const img = new Image();    //实例化一个图片img标签
                    img.src = file.result;     //图片的base 64 编码格式
                    img.width = 200;
                    box.appendChild(img)    //将标签添加入页面的box元素内
                }
            }
            e.preventDefault(); //阻止默认事件
            e.stopPropagation(); //阻止冒泡事件
            return false;
        }
    </script>
</body>
</html>
  • const oFile = e.dataTransfer.files.item(0);

  • const blob = new Blob( [oFile] );

  • const url = window.URL.createObjectURL(blob);

方法二,将图片处理成Blob对象方法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        html,body{
            width:100%;
            height:100%;
            display: flex;
            justify-content:center;
            align-items: center;
        }
        body{
            background-color: #ddd;
        }
        #box{
            width:80%;
            height:200px;
            background-color: deeppink;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>

        const box = document.getElementById('box');
        box.ondragover = function(e){   //检查有文件拖入触发
            e.preventDefault(); //阻止浏览器默认事件
            e.stopPropagation(); //阻止冒泡事件
            return false;
        }      
        box.ondrop = function(e){
            const dt = e.dataTransfer;
            const oFile = dt.files.item(0);	//读到文件对象的信息
            console.log( oFile )
            if( /image/.test(oFile.type) ){	//判断是否是图片
                const blob = new Blob( [oFile] );	//生成一个Blob对象格式
                const url = window.URL.createObjectURL(blob);//对二进制数据生成一个临时的url
                const img = new Image();	//生成一个图片标签对象
                img.src = url;				//给图片src填入路径
                img.width = 200;
                img.onload = function(){
                    box.appendChild( img )	//将图片添加到页面上
                }
            }
            e.preventDefault();
            e.stopPropagation();
            return false;

        }
    </script>
</body>
</html>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值