Detailed explanation of examples of H5 completing multiple image uploads

Y2J
Release: 2017-05-24 11:42:01
Original
3855 people have browsed it

This article mainly introduces the function of uploading multiple pictures based on HTML5, and the function of uploading multiple pictures on the basis of uploading a single picture. Interested friends can refer to

Picture Uploading I have written it before, but it was a single upload. Recently, there was a business requirement that required multiple uploads, so I rewrote the

HTML structure:


XML/HTMLCodeCopy the content to the clipboard

Copy after login

By the way, the main logic of this upload:

·Useinput tagand select type=file, remember to bring multiple, otherwise you can only select a single picture

·Bind the change time of the input,

·The key point is how to handle this changeevent. Use H5’s new FileReaderinterfaceto read the fileand encode it into base64. The next thing is Playing interactively with back-end classmates

JS code:


##JavaScriptCodeCopy Content to clipboard

window.onload = function(){ var input = document.getElementById("file_input"); var result,p; if(typeof FileReader==='undefined'){ result.innerHTML = "抱歉,你的浏览器不支持 FileReader"; input.setAttribute('disabled','disabled'); }else{ input.addEventListener('change',readFile,false); }
     //handler function readFile(){ for(var i=0;i          } var reader = new FileReader(); reader.readAsDataURL(this.files[i]); reader.onload = function(e){ result = '

'; p = document.createElement('p'); p.innerHTML = result; document.getElementById('body').appendChild(p);   //插入dom树
          } } } }
Copy after login

Is this how to upload multiple pictures? 0.0

However, it is not. This just converts the pictures into base64 encoding and then displays them on the front end. Once refreshed Nothing

After inserting the image, open the developer tools and see that the html structure is like this

The realistic approach is that we are dealing with the

function In, the file in the filequeueis sent to the backend. The backend student returns theMD5encrypted file and path corresponding to the file to the frontend, and the frontend takes this path. Rendered to the page.

Then transfer the MD5 file back to the backend, because after uploading, the frontend usually has the operation of

deletingpictures. The purpose of the return is to tell the backend to confirm that those pictures are what we want. , the backend is stored in the database.

Tell me how to interact with

jquery


##JavaScript Code

Copy content to the clipboard

function readFile(){ var fd = new FormData(); for(var i=0;i          } $.ajax({ url : '', type : 'post', data : fd, success : function(data){ console.log(data)  } }) }
Copy after login
FormData is also a new interface of H5, used to simulate the submission of form controls

. The biggest advantage is that you can submit binary filesand then call back the success After we get back the data we want, we can insert the picture into the page, similar to the previous method~

Last rendering:

The above is the entire content of this article, I hope it will be helpful to everyone's study.

【Related Recommendations】

1.

Html5 free video tutorial

2.

Example tutorial on the combined use of H5 and CSS3

3.

Detailed explanation of the event attributes of H5

4.

Detailed explanation of 28 very important new features, new techniques and new technologies of H5

5.

Code demonstration of making a timer in H5

The above is the detailed content of Detailed explanation of examples of H5 completing multiple image uploads. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!