Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preview selected image in input type="file" in popup using jQuery? [duplicate]

Tags:

html

jquery

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

The following is the input type I am using in my popup window.

HTML code:

<input type="file" name="uploadNewImage"> 
like image 789
Prasad Avatar asked Aug 27 '13 05:08

Prasad


People also ask

How do I preview the selected image in input type file?

const chooseFile = document. getElementById("choose-file"); const imgPreview = document. getElementById("img-preview");

How do you preview an image before it is uploaded using jquery?

Answer: Use the JS readAsDataURL() Method You can use the JavaScript readAsDataURL() method of the FileReader object to read the contents of the specified file. When the read operation is finished, the readyState becomes DONE, and the loadend is triggered. The FileReader result property returns the file's contents.

How display image from input type file react?

To display a image selected from file input in React, we can call the URL. createObjectURL with the selected file object to and set the returned value as the value of the src prop of the img element. We define the img state which we use as the value of the src prop of the img element.


1 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); }); 

Reference

like image 166
Prabhakaran Parthipan Avatar answered Oct 03 '22 01:10

Prabhakaran Parthipan