HTML Upload pictures Practical tips

Source: Internet
Author: User

Recently wrote the project needs to upload the image function but the browser comes with the button style is not bear to look straight, certainly to be modified, there are many methods on the Internet (self-check ...), I used a trickery method: is the function of the indirect call in the btn when clicked, let it perform the image selection function

Although the code is very simple but the effect is obvious, no longer need to worry about the selector style-casual design

HTML code

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "Utf-8" />        <title></title>        <Scripttype= "Text/javascript"src= "Js/choseimage.js" ></Script>        <style>        . Btn{width:100px;Height:50px;Border-radius:10px;Background-color:#72D5FB;position:Absolute;Top:300px; Left:300px;Outline:None;Border:None;        }#choseImage{Display:None;        }        </style>                            </Head>    <Body>        <inputtype= "File"ID= "Choseimage"onchange= "Showpreview (source)" />        <inputtype= "button"class= "Btn"value= "Select Picture"onclick= "Btns ()")/>        <Script>            functionBtns () {document.getElementById ("Choseimage"). Click (); }        </Script>    </Body></HTML>

JS Code

function Showpreview (source) {     var file = source.files[0];    if (!/image\/\w+/.test (File.type)) {        alert ("Make sure the file is an image type");        return false;    }    if (window. FileReader) {        var fr = new FileReader ();        Fr.readasdataurl (file);        Fr.onload = function (e) {                                                //The action to be performed when the picture is loaded successfully ~            document.getElementById ("Mybgimage"). src = e.target.result;                    ;    }}

HTML Upload pictures Practical tips

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.