Html5 sample code for reading local files

Source: Internet
Author: User

Comments: This article mainly introduces the implementation of html5 to read local files. The html structure style, Css style, and js Code are as follows. If you need them, you can see the following html structure style:

The Code is as follows:
<Div class = "addpic">
<Button> Add image </button>
<Form>
<Input id = "logoimg" class = "addlogo" type = "file" multiple accept = "image/*" name = "logo">
</Form>
</Div>


In terms of style, the input box of the input element should not be displayed. In this case, you need to set the input to a transparent style and overwrite it to the top of the button element before clicking the button to upload the image. If you set accepted to "image/*", only image files can be uploaded.

The Css style is as follows:

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

The Code is as follows:
Function readFiles (evt ){
Var files‑evt.tar get. files;
If (! Files ){
Console. log ("the file is invaild ");
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 );
}
}
}


The Code is as follows:
$ (Document). ready (function (){
$ ("# Logoimg"). change (function (e ){
ReadFiles (e)
});
});

Related Article

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.