简体   繁体   English

如何在使用javascript上传图像时显示图像

[英]How to display an image while uploading it using javascript

This function should display the image to upload.此函数应显示要上传的图像。 But on selecting a file it is not showing the image.但是在选择文件时,它不显示图像。

 function show_img(id_img) { if(this.files && this.files[0]) { var reader = new FileReader(); reader.onloadend = function(data) { var image = document.getElementById(id_img); image.src = data.target.result; } reader.readAsDataURL(this.files[0]); } } function create_img() { //create image var file_input = document.createElement('img'); file_input.id = "123_qwe"; file_input.height = "200"; file_input.width = "300"; contain.appendChild(file_input); //btn upload image var btn_image = document.createElement("INPUT"); btn_image.setAttribute("type", "file"); btn_image.name = "ref_img"; var id_image = "ref_img"; btn_image.id = id_image; contain.appendChild(btn_image); //Display image document.getElementById(id_image).addEventListener("onchange", function(){ show_img.call(id_image); console.log(id_image); }); }
 <div id="contain"> <Button id="btn_top" onclick="create_img();">create image</Button> </div>

How could I make the image appears in the image tag ?如何让图像出现在图像标签中? Thank you in advance.先感谢您。

here is the updated code.这是更新的代码。

Note: you have provided some wrong names to the variables.注意:您为变量提供了一些错误的名称。 make sure to correct them to decrease confusion.确保更正它们以减少混淆。

 function show_img(id_img, input) { if(input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(data) { var image = document.getElementById(id_img); image.src = data.target.result; } reader.readAsDataURL(input.files[0]); } } function create_img() { //create image var file_input = document.createElement('img'); file_input.id = "123_qwe"; file_input.height = "200"; file_input.width = "300"; contain.appendChild(file_input); //btn upload image var btn_image = document.createElement("INPUT"); btn_image.setAttribute("type", "file"); btn_image.name = "ref_img"; var id_image = "ref_img"; btn_image.id = id_image; contain.appendChild(btn_image); //Display image document.getElementById(id_image).addEventListener("change", function(e){ show_img(file_input.id, e.target); console.log(id_image); }); }
 <div id="contain"> <Button id="btn_top" onclick="create_img();">create image</Button> </div>

You forgot to declare an element and passed the wrong id to the second function.您忘记声明一个元素并将错误的 id 传递给第二个函数。 A working version of your code would be:您的代码的工作版本将是:

 function show_img(id_img, input) { if(input.files && input.files[0]){ var reader = new FileReader(); reader.onloadend = function(e) { document.getElementById(id_img).src = e.target.result; } reader.readAsDataURL(input.files[0]); } } function create_img(){ //create image var file_input = document.createElement('img'); file_input.id = "123_qwe"; file_input.height = "200"; file_input.width = "300"; var contain = document.getElementById("contain"); contain.appendChild(file_input); //btn upload image var btn_image = document.createElement("INPUT"); btn_image.setAttribute("type", "file"); var id_image = "ref_img"; btn_image.name = id_image; btn_image.id = id_image; contain.appendChild(btn_image); //Display image document.getElementById(id_image).onchange = function(){ show_img("123_qwe", this); }; }
 <div id="contain"> <button id="btn_top" onclick="create_img()">create image</button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM