Home  >  Article  >  Web Front-end  >  Simple image click upload function

Simple image click upload function

php中世界最好的语言
php中世界最好的语言Original
2018-03-08 11:14:188327browse

This time I will bring you a simple click-to-upload function for pictures, and make a simple click-to-upload function for pictures. What are the things to note? The following is a practical case, let’s take a look.

Simple image click upload preview example. Chrome, Firefox and other browsers can support drag and drop preview

<!DOCTYPE>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript"> 
//--从 file 域获取 本地图片 url
function getFileUrl(sourceId) {
    var url;
    if (navigator.userAgent.indexOf("MSIE")>=1) { // IE
        url = document.getElementById(sourceId).value;
    } else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox
        url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
    } else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome
        url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
    }
    return url;
}
var ss=null
var ImgType=["gif", "jpeg", "jpg", "bmp", "png","ico"];
//--将本地图片 显示到浏览器上 
function preImg(obj, targetId) { 
    ss=obj;
        if (!RegExp("\.(" +  ImgType.join("|") + ")$", "i").test(obj.value.toLowerCase())) {
            alert("选择文件错误,图片类型必须是" +  ImgType.join(","));
            this.value = "";
            return false
        }
    var sourceId=obj.id
    var url = getFileUrl(sourceId);
    var imgPre = document.getElementById(targetId);
    imgPre.src = url;
     
} 
</script>
</head>
 
<body>
    <form action="">
        <label style="position: relative;width:100px;height:100px;display: block;line-height:100px; " align="center">
             <input type="file"  accept="image/*" name="imgOne" id="imgOne" onchange="if(this.value){preImg(this, &#39;imgPre&#39;);}" style="height: 100%;width: 100%;position: absolute;z-index: 9;left: 0;right: 0;opacity: 0;"/>
             <img id="imgPre" src=""   style="display: block;position: absolute;width:100%;height:100%;border: 1px solid #999;"/>
             上传图片
        </label> 
    </form>
</body>
 
</html>

I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the PHP Chinese website!

Related reading:

Detailed explanation of the ng-bind-html directive of angularJS

Why do the front and back ends need to be written separately

The above is the detailed content of Simple image click upload function. For more information, please follow other related articles on the PHP Chinese website!

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