简体   繁体   English

Javascript:从浏览器中删除图像

[英]Javascript: Remove the image from browser

Following is the code I copied from the internet to show the preview of the image before uploading to the server. 以下是我从互联网复制的代码,以显示图像的预览,然后再上传到服务器。 If I click upload image it triggers click event on input element and uploads the image. 如果我单击上载图像,它将触发输入元素上的click事件并上载图像。 likewise if I cick on Delete I want to remove it from input element or from the web storage itself, I want to remove only that particular image so that it will not be processed by the server. 同样,如果我单击Delete(删除),则想从输入元素或Web存储本身中删除它,那么我只想删除该特定图像,这样服务器就不会对其进行处理。

<input type="file" id="files" />
<img id="image" />
<div id="uploadimage"> upload image</div>
<a href="#"> Delete</a>

<script>
document.getElementById("uploadimage").onclick=function(){
    document.getElementById("files").click();
};
document.getElementById("files").onchange = function () {
    var reader = new FileReader();

    reader.onload = function (e) {
        // get loaded data and render thumbnail.
        document.getElementById("image").src = e.target.result;

    };

    // read the image file as a data URL.
    reader.readAsDataURL(this.files[0]);
};
</script>

Thanks in advance 提前致谢

You can add id attribute to the link and complete the javascript with the following code: 您可以将id属性添加到链接,并使用以下代码完成javascript:

document.getElementById("delete").onclick = function () {
  document.getElementById("image").src = null;
  document.getElementById("files").value = '';
  return false;
};

jsfiddle jsfiddle

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

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