Html sample code for reading and displaying pictures in a local folder

Html sample code for reading and displaying pictures in a local folder

One purpose

Select a local folder on the Html page to automatically read all the images in the folder and its subfolders and display them on the page.

Technical Analysis:

Problems

  • The path obtained by the file tag in Html is relative.
  • When specifying the source in Img in Html, an absolute path is required.

Solution:

Call the readAsDataURL method in the Web API interface FileReader to read the data (the file path obtained by the function parameter file tag), and then load the data into FileReader (base64 format). After that, you can use Img to specify the source data in base64 format and draw pictures.

Second code

<!DOCTYPE html>
<html>
<head>
    <title>ReadImageDemo</title>
</head>
<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 stores 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>
</body>
</html>

Three effects

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Commonly used js function methods in the front end

>>:  Detailed explanation of scp and sftp commands under Linux

Recommend

Webpack builds scaffolding to package TypeScript code

Create a folder Directory structure: dabaots Init...

A brief discussion on JavaScript throttling and anti-shake

Table of contents Throttling and anti-shake conce...

How to specify parameter variables externally in docker

This article mainly introduces how to specify par...

Example analysis of the impact of MySQL index on sorting

This article uses examples to illustrate the impa...

Introduction to document.activeELement focus element in JavaScript

Table of contents 1. The default focus is on the ...

Sample code for making desktop applications with vue + Electron

1.vue packaging Here we use the vue native packag...

Summary of Node.js service Docker container application practice

This article will not explain the use and install...

A brief discussion on MySql views, triggers and stored procedures

view What is a view? What is the role of a view? ...

Simple usage example of MySQL 8.0 recursive query

Preface This article uses the new features of MyS...

js to realize login and registration functions

This article example shares the specific code of ...

Understand the implementation of Nginx location matching in one article

Since the team is separating the front-end and ba...

jQuery plugin to achieve code rain effect

This article shares the specific code of the jQue...

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...