How to view selected image in input type = "file" in popup menu using jQuery?

In my code, I allow the user to upload the image. Now I want to show this selected image as a preview in the same popup. How can I do this using jQuery?

Below is the type of input that I use in the popup.

HTML code:

<input type="file" name="uploadNewImage"> 
+42
jquery html5
Aug 27 '13 at 5:03 on
source share
5 answers

Demo

HTML:

  <form id="form1" runat="server"> <input type='file' id="imgInp" /> <img id="blah" src="#" alt="your image" /> </form> 

JQuery

 function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('#blah').attr('src', e.target.result); } reader.readAsDataURL(input.files[0]); } } $("#imgInp").change(function(){ readURL(this); }); 

Link

+110
Aug 27 '13 at 5:19
source share

If you are using HTML5, try running the code snippet

 <img id="uploadPreview" style="width: 100px; height: 100px;" /> <input id="uploadImage" type="file" name="myPhoto" onchange="PreviewImage();" /> <script type="text/javascript"> function PreviewImage() { var oFReader = new FileReader(); oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]); oFReader.onload = function (oFREvent) { document.getElementById("uploadPreview").src = oFREvent.target.result; }; }; </script> 
+23
Aug 27 '13 at 5:24
source share

You can use ajax download to preview the selected file. http://zurb.com/playground/ajax-upload

+1
Aug 27 '13 at 5:12
source share
 <script> function img_pathUrl(input){ $('#img_url')[0].src = (window.URL ? URL : webkitURL).createObjectURL(input.files[0]); } </script> <img src="" id="img_url" alt="your image"> <iput type="file" id="img_file" onChange="img_pathUrl(this);"> 
+1
Dec 15 '15 at 15:10
source share

Just make sure my scripts work well:

  function handleFileSelect(evt) { var files = evt.target.files; // FileList object // Loop through the FileList and render image files as thumbnails. for (var i = 0, f; f = files[i]; i++) { // Only process image files. if (!f.type.match('image.*')) { continue; } var reader = new FileReader(); // Closure to capture the file information. reader.onload = (function(theFile) { return function(e) { // Render thumbnail. var span = document.createElement('span'); span.innerHTML = ['<img class="thumb" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join(''); document.getElementById('list').insertBefore(span, null); }; })(f); // Read in the image file as a data URL. reader.readAsDataURL(f); } } document.getElementById('files').addEventListener('change', handleFileSelect, false); 
 #list img{ width: auto; height: 100px; margin: 10px ; } 
0
Jun 15 '16 at 12:38 on
source share



All Articles