About the API in H5, upload image preview function

Source: Internet
Author: User

FileReader: Reads the local picture file and displays

Write at the beginning

Before the company asked to do a H5 page, the function is to take photos and select photo albums, and can be previewed on the screen. Then I am silly smacking of the various find plug-ins, because some plug-ins do not fit the model of the phone, Android basically die, Apple sometimes will be a little bug (will flash back). That is the final solution is not to display the picture, the choice of the link to upload just fine. Know today I found Html5 inside there is a can upload pictures of the API, how not God!?
Let's talk about how it's going to come true.
No more talking nonsense, let's put the code to explain again

Html:

<P>    <label>Please select an image file:</label>    <inputtype= "File"ID= "File_input" />Select the input button for the picture</P>  <DivID= "RRR"></Div>It's got pictures.

Js:

varAAA = document.getElementById ("RRR");//gets the div element that displays the picturevarinput = document.getElementById ("File_input");//gets the input element of the selected picture            //This is to determine if the browser supports this API. if(typeoffilereader=== ' undefined ') {aaa.innerhtml= "Sorry, your browser does not support FileReader"; Input.setattribute (' Disabled ', ' disabled '); }Else{Input.addeventlistener (' Change ', ReadFile,false);//if support listens for change events, run the ReadFile function once the change is made. }       functionReadFile () {varFile = This. Files[0];//get File Object    //determines whether the type of file is a picture type.     if(!/image\/\w+/. Test (File.type)) {Alert ("The file must be a picture!" "); return false; }         varReader =NewFileReader ();//declaring a FileReader instanceReader.readasdataurl (file);//call the Readasdataurl method to read the selected image file    //Finally, in the OnLoad event, get the contents of the file that was read successfully and display the selected picture as an IMG nodeReader.onload =function(e) {aaa.innerhtml= '  This. result+ ' "/> '     } }

About the API in H5, upload image preview function

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.