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:
|
<<: Mysql uses insert to insert multiple records to add data in batches
>>: More Ways to Use Angle Brackets in Bash
Start cleaning carefully! List unused volumes doc...
Table of contents 1. Where is the slowness? 2. Ha...
text-shadow Add a shadow to the text. You can add...
When developing and debugging a web application, ...
1. Introduction When you encounter a requirement ...
The order in which the browser loads and renders H...
1. Command Introduction The watch command execute...
In MySQL, create a view on two or more base table...
Introduction: Nginx (pronounced the same as engin...
There is often a scenario where the image needs t...
Here are some points to note when registering Tom...
Table of contents 1. Create a table 1.1 Create te...
The innodb_autoinc_lock_mode parameter controls t...
Use scenarios: The project's pages need to lo...
Basic Concepts By default, Compose creates a netw...