zoukankan      html  css  js  c++  java
  • 读取文件/文件夹并回显

    </body>
    <input type="file" id="selectFiles" onchange="dealSelectFiles()" multiple webkitdirectory>
    <canvas id="myCanvas" width=1440 height=900></canvas>
    
    <script type="text/javascript">
        // 读取文件夹并回显
        var imgPosX = 0;
        var imgWidth = 256;
    
        function dealSelectFiles() {
            /// get select files.
            var selectFiles = document.getElementById("selectFiles").files;
    
            for (var file of selectFiles) {
                console.log(file.webkitRelativePath);
                /// read file content.
                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onloadend = function () {
                    /// deal data.
                    var img = new Image();
                    /// after loader, result storage the file content result.
                    img.src = this.result;
                    img.onload = function () {
                        var myCanvas = document.getElementById("myCanvas");
                        var cxt = myCanvas.getContext('2d');
                        cxt.drawImage(img, imgPosX, 0);
                        imgPosX += imgWidth;
                    }
                }
            }
        }
    </script>
    </html>
    
    <body>
    
    <input type="file" onchange="previewFile()"><br>
    <img src="" height="200" alt="Image preview...">
    <script>
        // 加载图片并并回显
        function previewFile() {
            // Where you will display your image
            var preview = document.querySelector('img');
            // The button where the user chooses the local image to display
            var file = document.querySelector('input[type=file]').files[0];
            // FileReader instance
            var reader = new FileReader();
    
            // When the image is loaded we will set it as source of
            // our img tag
            reader.onloadend = function () {
                preview.src = reader.result;
            }
    
    
            if (file) {
                // Load image as a base64 encoded URI
                reader.readAsDataURL(file);
            } else {
                preview.src = "";
            }
        }
    </script>
    
    
    </body>
    
  • 相关阅读:
    eBay要怎么转型?
    一张图让你看懂javascript各类型的关系
    闭包概念,匿名函数
    浅析Javascript闭包的特性
    深入理解JavaScript闭包(closure)
    深入理解JavaScript作用域和作用域链
    WPF 学习笔记(一)
    ChromiumWebBrowser flash不能自动播放问题解决方案
    饱含辛酸开发 WPF CustomControl
    KMP算法备忘
  • 原文地址:https://www.cnblogs.com/bbdbolg/p/14691384.html
Copyright © 2011-2022 走看看