Multiple image upload and preview
Asked Answered
R

3

7

I am learning how to upload multiple images and showing their preview...

I came across the following code

<html>
<head>
<style>
.input-file-row-1:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}

.input-file-row-1{
display: inline-block;
margin-top: 25px;
position: relative;
}

#preview_image {
display: none;
width: 90px;
height: 90px;
margin: 2px 0px 0px 5px;
border-radius: 10px;
}

.upload-file-container { 
position: relative; 
width: 100px; 
height: 137px; 
overflow: hidden;   
background: url('images/picplus.png') top center no-repeat;
float: left;
margin-left: 23px;
} 

.upload-file-container-text{
font-family: Arial, sans-serif;
font-size: 12px;
color: #719d2b;
line-height: 17px;
text-align: center;
display: block;
position: absolute; 
left: 0; 
bottom: 0; 
width: 100px; 
height: 35px;
}

.upload-file-container-text > span{
border-bottom: 1px solid #719d2b;
cursor: pointer;
}

.one_opacity_0 {
opacity: 0;
height: 0;
width: 1px;
float: left;
}
</style>
<script>
function readURL(input,target)
{
if(input.files && input.files[0])
{
var reader=new FileReader();
var image_target=$(target);
reader.onload=function(e)
{
    image_target.attr('src',e.target.result).show();
};
reader.readAsDataUrl(input.files[0]);
}
}

$("patient_pic").live("change",function(){
readURL(this,"#preview_image")
});
</script>

</head>

<body>
<form name="" method="post" action="#" class="feedback-form-1">
<fieldset>
    <div class="input-file-row-1">
        <div class="upload-file-container">
            <img id="preview_image" src="#" alt="" />
            <div class="upload-file-container-text">
                <div class = 'one_opacity_0'>
                    <input type="file" id="patient_pic" label = "add" />
                </div>
                <span> Add Photo </span>
            </div>

<div class="upload-file-container-text">
                <div class = 'one_opacity_0'>
                    <input type="file" id="patient_pic" label = "add" />
                </div>
                <span> Add Photo </span>
            </div>
    
</div>
    </div>
</fieldset>
</form>
</body>

</html>

I came across this JS Fiddle which explains it perfectly to me. But being a beginner I know it includes a jQuery library which clearly shows in framework extension of Fiddle. Now my issue is, how should I include it when I start the coding on my machine?

What will be included in the head (<script src="???">) section to make a call to the library?

Ritzy answered 26/12, 2013 at 6:9 Comment(0)
P
30

Below is a working example of a solution to preview multiple uploaded image. (Source, Fiddle)

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");
  }
}
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;
}
<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>
Permeable answered 25/4, 2014 at 14:22 Comment(8)
maraustria.wordpress.com/2014/04/25/…Permeable
the images being uploaded are shown in vertical alignment... how to show them horizontally.. like one after another not one below otherRitzy
.thumbnail{ float:left; height: 100px; margin: 10px; }Permeable
this is a wonderful solution.. thank you.. i hope it will work great if i integrate it with php... thanks a ton againRitzy
was wondering that how can i will be displaying name of the selected file below each thumbnail. can you helpRitzy
in js replace the div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "‘" + "title='" + picFile.name + "‘/>"; to div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" + "title='" + file.name + "'/>" + "<span>" + file.name +"<span>"; that's it.Permeable
file name are appearing in fiddle... but they aint exactly below it.. they are appearing at bottom rightRitzy
I updated the code: jsfiddle.net/2xES5/28 or maraustria.wordpress.com/2014/04/25/…Permeable
S
1

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()

Seko answered 26/12, 2013 at 6:13 Comment(2)
Thank you for reply i did it but still image previews are not displayedRitzy
any ideas why preview is not being displayed?Ritzy
E
0

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>
Elutriate answered 26/12, 2013 at 6:19 Comment(9)
thank you... any idea why the images are not being previewed after uploading them??Ritzy
Where are you trying to do this? Are you talking about multiple Image Preview not happening in the Fiddle?Elutriate
i copied the code in notepad and saved it with .html extension... it shows the output with plus symbol.. but wheni actually upload an image.. it doesn't shows the previewRitzy
You want the Code implementation in a single file?Elutriate
no not needed.. have decided to seperate the css in css named folderRitzy
when i actually do this.. it doesnt shows the preview of imageRitzy
hei there.. now the things are working up.. but as i try to preview multiple images.. i get preview at only first section... here is the fiddle jsfiddle.net/arpymastro/SFHF6Ritzy
I suppose you already got the correct answer form the first answer... since it is marked correct right?Elutriate
yes it did resolved my first issue... but now another issue is.. wheni try multiple uploading of images.. only one get previewed.. here is the fiddle- jsfiddle.net/arpymastro/SFHF6Ritzy

© 2022 - 2024 — McMap. All rights reserved.