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
- <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:
Copy the content to the clipboard using JavaScript 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 );
- } <Br> // 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") <br>}
- 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 a dom tree <br>}
- }
- }
- }
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
- 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]); <br>}
- $. 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:
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