1

I would like to read the URL of an image selected in an <input type="file">tag and then display the corresponding image without uploading the file to the server first.

<input type='flie'id='anything' name='anything'>

and outputted in

<div id='showimg'></div>

without uploading it first.

8
  • try googling for upload file using ajax
    – JoSSte
    Commented Nov 15, 2018 at 13:48
  • 1
    @JoSSte you may as well recommend Stack Overflow internal search: stackoverflow.com/search?q=upload+ajax
    – Cœur
    Commented Nov 15, 2018 at 14:03
  • its not helpful.. i just want img in that div when i choose some pic from input type file.. by using onchange function..
    – P P
    Commented Nov 15, 2018 at 15:45
  • "Upload" infers transferring an image from the client to the server. If you just want to display the image, that is ENTIRELY different.
    – JoSSte
    Commented Nov 16, 2018 at 8:59
  • @JoSSte instead of writing comment, flag question as duplicate - this results in comment like mine above.
    – barbsan
    Commented Nov 16, 2018 at 10:06

1 Answer 1

1

"Upload" infers transferring an image from the client to the server. If you just want to display the image, that is ENTIRELY different. what you want is to have your <input type=image>, and have an onchange action which reads the path of the image, and then takes that path and puts in the src part of an img tag.

Something like this: (no guarantee for this to work. look up the specifics...)

<html>
<head>
<script>
function updateimgref(){
  var img = document.getElementById("outputimg");
  var input = document.getElementById("imginput");
  if (input.files && input.files[0]) {
    img.href = input.result;
  }

}
</script>
</head>
<body>
<input type="image" id="imginput" onchange="updateimgref()">
<img id="outputimg" href="myblankplaceholder.png">
</body>
</html>

se also HTML input type=file, get the image before submitting the form

2
  • Tysm man...(y)..
    – P P
    Commented Nov 20, 2018 at 5:45
  • if it helped, mark it as accepted answer ;-)
    – JoSSte
    Commented Nov 20, 2018 at 16:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.