Html5 uploads general code for image mobile and PC terminals,
I will not talk much about it. I will share with you today that html5 uploads images. We use it on mobile terminals, but I only tested the General compatibility on pc. I used angular to write "Upload image preview with HTML5 File API". Today, we will share an html5 + js Image Upload case with angular. So let's take some steps today.
HTMLStep 1: Create html. We will place the input # upload selected for a file on the page (PS: lazy. Here we will not write the case again and copy our page directly)
Copy the content to the clipboard using CSS Code.
- <Div class = "con4">
- <Span class = "btn upload"> upload <input type = "file" class = "upload_pic" id = "upload"/> </span>
- </Div>
CSSNote: css is a bit messy. If you cannot understand it, you can ask me, or simply write it on your own.
Copy the content to the clipboard using CSS Code.
- Con {
- Width: %;
- Height: auto;
- Overflow: hidden;
- Margin: % auto;
- Color: # FFFFFF;
- }
- Con. btn {
- Width: %;
- Height: px;
- Line-height: px;
- Text-align: center;
- Background: # dbc;
- Display: block;
- Font-size: px;
- Border-radius: px;
- }
- Upload {
- Float: left;
- Position: relative;
- }
- Upload_pic {
- Display: block;
- Width: %;
- Height: px;
- Position: absolute;
- Left :;
- Top :;
- Opacity :;
- Border-radius: px;
- }
Javascript
Get the node through getElementById to determine the browser compatibility. If the browser does not support the FileReader interface, a prompt is provided and the input is disabled. Otherwise, the system listens to the change event of the input.
Copy the content to the clipboard using JavaScript Code
- // Obtain the upload button
- Var input = document. getElementById ("upload ");
- If (typeof FileReader === 'undefined '){
- // Result. innerHTML = "sorry, your browser does not support FileReader ";
- Input. setAttribute ('Disabled ', 'Disabled ');
- } Else {
- Input. addEventListener ('change', readFile, false );
- }
Then, when the change event of file_input is triggered, the readFile () function is called (). In readFile, we first obtain the file object, and then use the type attribute of file to detect the file type. Of course, we can only select an image file, and then we create a new FileReader instance, call the readAsDataURL method to read the selected image file. In the onload event, obtain the successfully Read File Content and insert an img node to display the selected image.
Copy the content to the clipboard using JavaScript Code
- Function readFile (){
- Var file = this. files [];
- If (! /Image \/\ w +/. test (file. type )){
- Alert ("the file must be an image! ");
- Return false;
- }
- Var reader = new FileReader ();
- Reader. readAsDataURL (file );
- // When the file is read successfully, you can retrieve the upload interface and specify the path to upload (PS: You can secretly send your photos to me !)
- Reader. onload = function (e ){
- Var data = this. result. split (',');
- Var tp = (file. type = 'image/png ')? 'Png ': 'jpg ';
- Var a = data [];
- // Ajax requests can be made here for uploads to the server
- ......
- }
- };
Here we have completed the image upload function. If you are interested, please try it by yourself. If you do not understand it, or if I am wrong, please contact me. FileReader methods and events