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