Web APIs - FileReader - FileReader.readAsDataURL()

FileReader.readAsDataURL()

The readAsDataURL method is used to read the contents of the specified Blob or File. When the read operation is finished, the readyState becomes DONE, and the loadend is triggered. At that time, the result attribute contains the data as a data: URL representing the file’s data as a base64 encoded string.

Note: The blob’s result cannot be directly decoded as Base64 without first removing the Data-URL declaration preceding the Base64-encoded data. To retrieve only the Base64 encoded string, first remove data:*/*;base64, from the result.

Syntax

instanceOfFileReader.readAsDataURL(blob);

Parameters

  • blob

    The Blob or File from which to read.


选择、读取单个文件并预览

在这里插入图片描述

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <input type="file" />
        <br />
        <img src="" height="200" alt="Image preview..." />

        <script type="text/javascript">
            const input = document.querySelector('input[type=file]')
            const image = document.querySelector('img')
            
            console.log(input, image)
            
            input.onchange = (event) => {
                // console.log(event)
                const files = event.target.files
                
                if (files.length > 0) {
                    const fileReader = new FileReader()
                    
                    fileReader.onload = (event) => {
                        // console.log(event)
                        image.src = fileReader.result
                    }
                    
                    fileReader.readAsDataURL(files[0])
                }
            }
        </script>
    </body>
</html>

选择、读取多个文件并预览

在这里插入图片描述

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Reading multiple files</title>
    </head>
    <body>
        <input type="file" multiple />
        <br />
        <div id="preview"></div>

        <script type="text/javascript">
            const input = document.querySelector('input[type=file]')
            const preview = document.querySelector('#preview')

            console.log(input, preview)

            input.onchange = (event) => {
                // console.log(event)
                const files = event.target.files

                for (let i = 0; i < files.length; i++) {
                    const file = files[i]

                    if (/\.(jpe?g|png|gif)$/i.test(file.name)) {
                        const fileReader = new FileReader()
                        
                        fileReader.addEventListener("load", function(event) {
                            console.log(event, this)
                            const image = new Image()
                            image.height = 100
                            image.title = file.name
                            image.style = "margin-right: 5px;"
                            image.src = this.result
                            preview.appendChild(image)
                        }, false)
                        
                        fileReader.readAsDataURL(file)
                    }
                }
            }
        </script>
    </body>
</html>

参考

FileReader.readAsDataURL()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值