js to upload pictures to the server

js to upload pictures to the server

This article example shares the specific code of js to upload pictures to the server for your reference. The specific content is as follows

HTML

//Upload multiple pictures multiple
<input type="file" id="file" multiple>
//Native submit button <input type="submit">

javascript

<script>
    // Define an array to receive images converted to base64 let ArrayImg=[]
    var index = 0; //Add a unique identifier to the image for easy deletion // Get the element on the page let input=document.getElementById('file')
    console.log(input);
    // Bind oncheange event input.onchange=function(){
      var file = this.files[0] //Get the file selected on the page [N] refers to the number of files to get // console.log(file);
      var iLen = this.files.length; //Get the image length// console.log(iLen);
      for(var i=0;i<iLen;i++){ //Display multiple images on the page or upload them through a loop //Preview local cache var filereader = new FileReader() //Create a local cache object //Convert the local cache of the acquired file to bese64
      filereader.readAsDataURL(this.files[i]) //Convert to base64 and store in free attribute reader.result console.log([i]);
        filereader.onload = function () { //Get this.result through onload event // console.log(this.result,333);
        ArrayImg.push(this.result)
        // Include the img display image through the html tag and store it in a variable let img1=`<div id="divimg"><img src="${this.result}" alt="" id="id_img"></div>`  
        // Create a new div
        let div = document.createElement('div')
        div['index'] = index; // Add a unique identifier to div for easy removal // Put the uploaded img1 into the newly created div div.innerHTML=img1
        console.log(ArrayImg,'image array');
        //Then insert it into the DOM tree through DOM operation to display the picture document.getElementsByTagName('body')[0].appendChild(div) //Insert into the DOM tree// console.log(img);
        // Delete the currently clicked div and the base64 address in the currently clicked image array by binding the click event to div div.onclick = function(){  
                    this.remove(); // Delete the image element in the page delete ArrayImg[this.index]; // Delete the data corresponding to the ArrayImg array console.log(ArrayImg,'Image array');
                }
                //inddex records how many times the current loop is done to remove the link address index++ in the ArrayImg array
      }
      }
    }
</script>

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.

You may also be interested in:
  • js to upload pictures to the server and echo them
  • How to upload files and images in Node.js HTTP server
  • JSP+Servlet realizes the function of uploading files to the server
  • NodeJs implements a simple WEB upload and download server
  • Detailed explanation of Node.js one-line command to upload local files to the server
  • js to achieve the example of uploading pictures to the server and displaying them
  • Realize the function of uploading a single file to the server based on HTML5+js+Java
  • Use nodejs to monitor file changes and upload them to the server using sftp
  • NodeJS combined with HTML5 to achieve the method of dragging and dropping multiple files to upload to the server
  • Ajax upload implements js processing based on data returned by the server

<<:  Mysql uses insert to insert multiple records to add data in batches

>>:  More Ways to Use Angle Brackets in Bash

Recommend

Docker cleanup environment operation

Start cleaning carefully! List unused volumes doc...

Reasons why MySQL queries are slow

Table of contents 1. Where is the slowness? 2. Ha...

Implementation of CSS text shadow gradually blurring effect

text-shadow Add a shadow to the text. You can add...

Nginx cache configuration example

When developing and debugging a web application, ...

Sample code for highlighting search keywords in WeChat mini program

1. Introduction When you encounter a requirement ...

A detailed introduction to HTML page loading and parsing process

The order in which the browser loads and renders H...

Use of Linux watch command

1. Command Introduction The watch command execute...

How to create a view on multiple tables in MySQL

In MySQL, create a view on two or more base table...

Analysis of the process of implementing Nginx+Tomcat cluster under Windwos

Introduction: Nginx (pronounced the same as engin...

Several ways to implement image adaptive container with CSS (summary)

There is often a scenario where the image needs t...

Summary of some points to note when registering Tomcat as a service

Here are some points to note when registering Tom...

MySQL quickly inserts 100 million test data

Table of contents 1. Create a table 1.1 Create te...

About MySQL innodb_autoinc_lock_mode

The innodb_autoinc_lock_mode parameter controls t...

Nginx content cache and common parameter configuration details

Use scenarios: The project's pages need to lo...

Docker Compose network settings explained

Basic Concepts By default, Compose creates a netw...