HTML5 allows you to upload multiple images,

Source: Internet
Author: User
Tags readfile

HTML5 allows you to upload multiple images,

I have also written this 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:

Copy XML/HTML Code to clipboard
  1. <Div class = "container">
  2. <Label> select an image file: </label>
  3. <Input type = "file" id = "file_input" multiple/>
  4. </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:

Copy the content to the clipboard using JavaScript Code
  1. Window. onload = function (){
  2. Var input = document. getElementById ("file_input ");
  3. Var result, div;
  4. If (typeof FileReader === 'undefined '){
  5. Result. innerHTML = "sorry, your browser does not support FileReader ";
  6. Input. setAttribute ('Disabled ', 'Disabled ');
  7. } Else {
  8. Input. addEventListener ('change', readFile, false );
  9. } <Br> // handler
  10. Function readFile (){
  11. For (var I = 0; I <this. files. length; I ++ ){
  12. If (! Input ['value']. match (/. jpg |. gif |. png |. bmp/I) {// determine the format of the uploaded file
  13. Return alert ("the format of the uploaded image is incorrect. Please reselect") <br>}
  14. Var reader = new FileReader ();
  15. Reader. readAsDataURL (this. files [I]);
  16. Reader. onload = function (e ){
  17. Result = '<div id = "result"> </div> ';
  18. Div = document. createElement ('div ');
  19. Div. innerHTML = result;
  20. Document. getElementById ('body'). appendChild (div); // insert a dom tree <br>}
  21. }
  22. }
  23. }

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.

Copy the content to the clipboard using JavaScript Code
  1. Function readFile (){
  2. Var fd = new FormData ();
  3. For (var I = 0; I <this. files. length; I ++ ){
  4. Var reader = new FileReader ();
  5. Reader. readAsDataURL (this. files [I]);
  6. Fd. append (I, this. files [I]); <br>}
  7. $. Ajax ({
  8. Url :'',
  9. Type: 'post ',
  10. Data: fd,
  11. Success: function (data ){
  12. Console. log (data)
  13. }
  14. })
  15. }

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:

The above is all the content of this article, hoping to help you learn.

Original article: http://www.cnblogs.com/weapon-x/p/5237064.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.