最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display image from a given url - Stack Overflow

programmeradmin3浏览0评论

I have an array with 3 cells.At the first cell i have a textarea where you can insert the url of an image.At the second cell i have a button which when you click the image display at the third cell where i have a div to display the image.The question is how can i display the image either from the internet either from local? The code i wrote is:

function loadImage(){
  var mydiv = document.getElementById("idofdivtodisplayimg");
  var url = document.getElementById("idoftextareawhereyouputtheurl");
  mydiv.innerHTML = url.value;
}

I have an array with 3 cells.At the first cell i have a textarea where you can insert the url of an image.At the second cell i have a button which when you click the image display at the third cell where i have a div to display the image.The question is how can i display the image either from the internet either from local? The code i wrote is:

function loadImage(){
  var mydiv = document.getElementById("idofdivtodisplayimg");
  var url = document.getElementById("idoftextareawhereyouputtheurl");
  mydiv.innerHTML = url.value;
}
Share Improve this question asked May 29, 2018 at 15:08 andrikoulasandrikoulas 871 gold badge1 silver badge7 bronze badges 5
  • What does "from the local" mean? Upload the file? – Luca Kiebel Commented May 29, 2018 at 15:10
  • basically display an image witch is at your local pc – andrikoulas Commented May 29, 2018 at 15:11
  • Okay, and what have you tried in order to make that work? – Luca Kiebel Commented May 29, 2018 at 15:11
  • I wrrote the function i tried above – andrikoulas Commented May 29, 2018 at 15:13
  • Using only a textarea / text input you can't as you cannot access a file on the local system simply by the user giving a path. You would need to implement a file input element or a drag and drop scheme for that portion of your idea to work – Patrick Evans Commented May 29, 2018 at 15:15
Add a ment  | 

5 Answers 5

Reset to default 0
<html>
<body>
    <input type="text" id="imagename" value="" />
    <input type="button" id="btn" value="GO" />
    <script type="text/javascript">
        document.getElementById('btn').onclick = function() {
            img = document.createElement('img');
            img.src = document.getElementById('imagename').value;
            document.body.appendChild(img);
        }
    </script>
</body>
</html>

You can see the sample code will add the images from an array to the document. You could also append the images to any of the elements in your function by using url.appendChild

  var arr = ['http://via.placeholder./350x150', 'http://via.placeholder./350x250','http://via.placeholder./350x350']; // hold image urls in an array.

  arr.forEach(function(item){
      // loop through array and add images to the document.
      var img = new Image();
      img.src = item;
      document.body.appendChild(img);
  });

In order to do both you would need to change your html and code.

For the case when the user has a url you can just create a new image and append it to your div setting the image's src to the url that was set in the input:

function loadImage(){
  var mydiv = document.getElementById("idofdivtodisplayimg");
  var url = document.getElementById("idoftextareawhereyouputtheurl");
  var image = new Image;
  mydiv.appendChild(image);
  image.src = url.value;
}

Now to get it to display a local image you will need a file input or a drag and drop scheme as you cannot access local files without some type of user interaction.

So you would, for example, need to change your html to include a file input, and grab a reference to the selected file the user selects. Then use FileReader to read the file, and finally display it

HTML

<input type="file" id="imagefile">

JS

//input reference
var imageinput = document.getElementById("imagefile");
imageinput.addEventListener('change',function (){
  var mydiv = document.getElementById("idofdivtodisplayimg");

  var image = new Image;
  mydiv.appendChild(image);

  //FileReader instance
  var reader = new FileReader;
  reader.addEventListener('load',function(){
     //reader.result will contain a dataURL that can be used
     //like a regular image url
     image.src = reader.result;
  });
  //read the file as a dataURL
  reader.readAsDataURL( imageinput.files[0] );
});

This does both, it let's you upload an image (or at least load it to the browser) or give a URL to an image source. Click the button and the image is loaded and displayed!

This snippet uses the FileReader API to get the uploaded image and display it in an image element

function uploadOrNot() {
  if (document.querySelector("input[type=file]").files[0]){
    let input = document.querySelector("input[type=file]");
    if (input.files && input.files[0]) {
      var reader = new FileReader();

      reader.onload = function(e) {
        display(e.target.result);
      }

      reader.readAsDataURL(input.files[0]);
    }
  } else if (document.querySelector("input[type=text]").value){
     display(document.querySelector("input[type=text]").value);
  }
}

function display(res) {
	let img = document.createElement("IMG");
	img.src=res;
	document.querySelector("#result").appendChild(img);
}
<div id="urlOrUpload">
  <input type="text"/>
  <br> 
  <input type="file" accetp="image/*"/>
</div>
<div id="buttonHolder">
  <button type="button" onclick="uploadOrNot()">Display</button>
</div>
<div id="result"></div>

I partly solved it by replacing the div at the third cell with an img tag and at the function i wrote above, i chenged it to:

var image = document.getElementbyId("imageid");
var url = document.getElementbyId("urlid");
image.src = url.value;

But at the table i have,i also have a button where you can add a same row as above.How can i do this function for every url that is placed at every textbox?

发布评论

评论列表(0)

  1. 暂无评论