Home  >  Article  >  Web Front-end  >  HTML5 reads local file sample code_html5 tutorial skills

HTML5 reads local file sample code_html5 tutorial skills

WBOY
WBOYOriginal
2016-05-16 15:47:591651browse

The html structure style is as follows:

Copy code
The code is as follows:









In terms of style, the input box of the input element should not be displayed. In this case, the input needs to be set to a transparent style, and then covered over the button element. Only then can the button be clicked to upload the image. . Set accepted to "image/*" to allow only image files to be uploaded.

Css style is as follows

Copy code
The code is as follows:

.addpic{
position:relative;
margin-left:100px;
width:95px;
height:30px;
}
.addlogo {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
cursor: pointer;
font-size: 30px;
opacity: 0;
position: absolute;
right: 0 ;
top: 0;
z-index: 10;
}

js code

Copy the code
The code is as follows:

function readFiles(evt){
var files=evt.target.files;
if(!files){
console.log("the file is invalid");
return;
}
for(var i=0, file; file=files[i]; i ){
var imgele=new Image();
var thesrc=window.URL.createObjectURL(file);
imgele.src=thesrc;
imgele.onload=function(){
$("#showlogo ").attr("src",this.src);
}
}
}


Copy the code
The code is as follows:

$(document).ready(function(){
$("#logoimg").change(function(e ){
readFiles(e)
});
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn