HTML5-based upload of multiple images,
I have also written a demo before uploading images, but it is just a single upload. Recently, I have a business requirement that I need to upload multiple images, so I almost changed it.
HTML structure:
<Div class = "container"> <label> select an image file: </label> <input type = "file" id = "file_input" multiple/> </div>
By the way, the main logic of this upload is as follows:
· Use the input tag and select type = file. Remember to include multiple. Otherwise, you can only select one image.
· Bind the change time of input,
· The key point is how to deal with this change event. Use the new FileReader interface of H5 to read files and encode the files in base64. Then, you can interact with the back-end students.
JS Code:
Window. onload = function () {var input = document. getElementById ("file_input"); var result, div; if (typeof FileReader = 'undefined') {result. innerHTML = "sorry, your browser does not support FileReader"; input. setAttribute ('Disabled ', 'Disabled');} else {input. addEventListener ('change', readFile, false );}
// Handler function readFile () {for (var I = 0; I <this. files. length; I ++) {if (! Input ['value']. match (/. jpg |. gif |. png |. bmp/I) {// determine the format of the uploaded file return alert ("the format of the uploaded image is incorrect. Please reselect ")
} Var reader = new FileReader (); reader. readAsDataURL (this. files [I]); reader. onload = function (e) {result = '<div id = "result"> </div>'; div = document. createElement ('div '); div. innerHTML = result; document. getElementById ('body '). appendChild (div); // Insert the dom tree
}}}}
Is this true for uploading multiple images? 0.0
However, this is not the case. It only converts the image to base64 encoding before the front-end display. There is nothing to refresh.
After inserting an image, open the developer tool to view the html structure.
In practice, we send the files in the file queue to the backend in the processing function. the backend students return the MD5 encrypted file and path corresponding to the file to the front-end, the front end renders the image to the page with this path.
Then, the MD5 file is sent back to the backend, because after the upload, the front-end usually deletes the image. The purpose of the return is to tell the backend to confirm that the image is what we want and store it in the database.
Let's talk about how to interact with jquery.
function readFile(){ var fd = new FormData(); for(var i=0;i<this.files.length;i++){ var reader = new FileReader(); reader.readAsDataURL(this.files[i]); fd.append(i,this.files[i]);
} $.ajax({ url : '', type : 'post', data : fd, success : function(data){ console.log(data) } })}
FormData is also a new H5 interface used to simulate the submission of form controls. The biggest benefit is that it can submit binary files.
Then, after we get the desired data in the success callback, We can insert the image into the page, similar to the previous practice ~
Previous:
I have a relatively low level of qualifications, which may lead to mistakes. You are welcome to correct your questions and exchange ideas.