multiple image upload and preview

南楼画角 提交于 2019-12-03 00:44:59

I have a solution to preview multiple image upload https://maraustria.wordpress.com/2014/04/25/multiple-select-and-preview-of-image-of-file-upload/

Html

<form id="post-form" class="post-form" method="post">
<label for=”files”>Select multiple files: </label>
<input id=”files” type=”file” multiple/>
<output id=”result” />
</form>

Javascript

window.onload = function(){
    //Check File API support
    if(window.File && window.FileList && window.FileReader)
    {
        var filesInput = document.getElementById(“files”);
        filesInput.addEventListener(“change”, function(event){
            var files = event.target.files; //FileList object
            var output = document.getElementById(“result”);
            for(var i = 0; i< files.length; i++)
            {
                var file = files[i];
                //Only pics
                if(!file.type.match(‘image’))
                    continue;
                var picReader = new FileReader();
                picReader.addEventListener(“load”,function(event){
                    var picFile = event.target;
                    var div = document.createElement(“div”);
                    div.innerHTML = “<img class=’thumbnail’ src=’” + picFile.result + “‘” +
                    “title=’” + picFile.name + “‘/>”;
                    output.insertBefore(div,null);
                });
                //Read the image
                picReader.readAsDataURL(file);
            }
        });
    }
    else
    {
        console.log(“Your browser does not support File API”);
    }
}

Css

body{
font-family: ‘Segoe UI’;
font-size: 12pt;
}

header h1{
font-size:12pt;
color: #fff;
background-color: #1BA1E2;
padding: 20px;

}
article
{
width: 80%;
margin:auto;
margin-top:10px;
}

.thumbnail{

height: 100px;
margin: 10px;
}

http://jsfiddle.net/0GiS0/Yvgc2/

First include Jquery library in your <head> section

<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>

Then below that

<script>
$(function(){

//Here your function

});
</script>

Change in your function use on instead of live

$("patient_pic").on("change",function(){
readURL(this,"#preview_image")
});

jQuery has deprecated live() since 1.7, instead use on()

Its as simple as :-

<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
<script>

$(document).ready(function(){

//Your Code Goes Here

});

</script>
</head>
<body>
<!--Your H
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!